动态生成多个VBE按钮控件并操作它们的属性
2011-12-23 14:39
471 查看
实现环境:Visual Studio 2010,Excel 2010, VSTO 4.0
这似乎是唯一行得通的方法了,因为在Worksheet中我们是没办法操作控件属性的。
相关资源:http://download.csdn.net/detail/tx_officedev/3961969
Imports Microsoft.Office.Tools.Ribbon Imports VBE = Microsoft.Vbe.Interop Imports Forms = Microsoft.Vbe.Interop.Forms Public Class Ribbon1 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 objApplication As Excel.Application = Globals.ThisAddIn.Application Dim objWorkbook As Excel.Workbook = objApplication.ActiveWorkbook Dim objWorksheet As Excel.Worksheet = objWorkbook.ActiveSheet Dim iButtonNumber As Integer = CInt(EditBox1.Text) Dim iHeight As Integer = 25 Dim iWeight As Integer = 125 Dim iTop As Integer = 0 Dim iLeft As Integer = 0 Dim objShape As Excel.Shape Dim objOLEObject As Excel.OLEObject Dim objCommandButtonControls As CommandButtonControls For i As Integer = 1 To iButtonNumber Step 1 objShape = objWorksheet.Shapes.AddOLEObject _ ("Forms.CommandButton.1", Left:=iLeft, _ Top:=iTop + iHeight * (i - 1), Height:=iHeight, Width:=iWeight) objShape.Name = "btn" & CStr(i) objOLEObject = objWorksheet.OLEObjects("btn" & CStr(i)) objCommandButtonControls = New CommandButtonControls _ (objShape.Name, "This is button" & objShape.Name) objCommandButtonControls.objCommandButton = TryCast _ (objOLEObject.Object, Forms.CommandButton) objCommandButtonControls.Init() Next i End Sub End Class Public Class CommandButtonControls Public WithEvents objCommandButton As Forms.CommandButton Public Name As String Public Caption As String Public Sub New(ByVal Name As String, ByVal Caption As String) Me.Name = Name Me.Caption = Caption End Sub Public Sub Init() objCommandButton.Caption = Caption End Sub Private Sub objCommandButton_Click() Handles objCommandButton.DblClick MsgBox(Name & Chr(13) & Caption) If Name.Equals("btn3") Then objCommandButton.Font.Bold = True End If End Sub End Class
这似乎是唯一行得通的方法了,因为在Worksheet中我们是没办法操作控件属性的。
相关资源:http://download.csdn.net/detail/tx_officedev/3961969
相关文章推荐
- 动态生成多个VBE按钮控件并操作它们的属性
- asp.net C#动态添加创建生成button按钮控件没有触发执行响应click绑定事件处理
- asp.net点击按钮动态生成文本框控件
- 使用WebElements定位多个相似的元素,比如页面中存在五个单选按钮,他们有相同的class属性,值为:myRadio,我们想对五个按钮循环操作,我们可以把它们全部取出来放到集合中,然后做循环操作
- 【转】ASP.NET中服务器控件Table动态生成表格及其属性介绍
- QTP如何设置动态属性的标准检查点? 此业务是:点击【生成验证码】按钮,会在一个文本框中生成验证码
- android实现动态生成控件(setTag和getTag属性的用法)
- 动态生成 控件 并进行遍历和操作
- 动态生成 控件 并进行遍历和操作
- Asp.Net Table控件动态生成表格操作实例(代码调试通过)
- 控件的动态生成与操作
- asp.net动态生成按钮,点击按钮执行相关操作
- spring aop之动态生成jquery-easyui数据列表操作按钮
- ASP.NET中服务器控件Table动态生成表格及其属性介绍
- PropertyGrid控件动态生成属性及下拉菜单 (转)
- 动态生成服务器控件,点击按钮获取
- c# 动态生成 控件
- C# webBrowser 控件 操作网页的的按钮
- [iOS开发项目-3] 按钮控件的移动,放大缩小,左右旋转操作
- 动态生成的控件,怎样定义它的事件