向Excel文档中嵌入VBA控件和UserForm并显示
实现环境:Visual Studio 2010, Excel 2010, VSTO 4.0
在文档中嵌入一个Commandbutton点击它会显示一个UserForm,UserForm上有一个CommandButton点击会弹出消息框然后CommandButton的Caption改变。当UserForm关闭是文档中的CommandButton的Caption改变。
VB.NET:
[vb]
Imports Microsoft.Office.Tools.Ribbon
Imports VBE = Microsoft.Vbe.Interop
Imports Forms = Microsoft.Vbe.Interop.Forms
Public Class Ribbon1
Private WithEvents objCommandButton As Forms.CommandButton
Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As _
RibbonUIEventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _
As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
Dim objApplicatin As Excel.Application = Globals.ThisAddIn.Application
Dim objWorkbook As Excel.Workbook = objApplicatin.ActiveWorkbook
Dim objWorksheet As Excel.Worksheet = objWorkbook.ActiveSheet
Dim objShape As Excel.Shape
Dim objOLEObject As Excel.OLEObject
Dim strModuleSnippet As String
Dim objVBAProject As VBE.VBProject
Dim objVBComponent As VBE.VBComponent
Dim objVBFormComponent As VBE.VBComponent
Dim objObjectFormButton As Object
objShape = objWorksheet.Shapes.AddOLEObject("Forms.CommandButton.1")
objShape.Name = "btn1"
objOLEObject = objWorksheet.OLEObjects("btn1")
Try
objCommandButton = TryCast(objOLEObject.Object, Forms.CommandButton)
objCommandButton.Caption = "Click Me"
strModuleSnippet = "private sub btn1_Click()" & Chr(13) & _
"UserForm1.Show " & Chr(13) & "end sub"
相关新闻>>
- asp.net导出Excel方法总结
- DataTable导出到Excel时身份证号信息自动转换为科学计数法的解决
- asp.net中操作excel等不存在类型或命名空间名称Office
- 使用VSTO向Word文档中添加数学公式
- 连接Excel数据源并查询
- 用OpenXml在文档的尾部添加一个Rich Text Content Control/Plain
- 用OpenXml在文档的尾部添加一个Rich Text Content Control/Plain
- ASP.NET 显示 Excel 文件的分页方法
- 两种方法接口aspx页面的编写(页面展示xml文档)
- ASP.NET MVC 将IList<T>导出Excel文档的泛型类(继承自Action
- 发表评论
-
- 最新评论 更多>>