我们编写COM加载项就是要在Excel里面做点什么,如果用一个变量来保存Excel对象的话,我们基本上就能任意把Excel捏圆捏扁。变量的作用域是作为模块级的还是全局的就看你的实际情况,为了方便,这里定义为全局变量。添加一个模块“mduMain”,在模块中定义变量: - Public gExcelApp As Excel.Application。
复制代码 连接Excel对象
双击设计器“Connect”,按“F7”进入代码编辑窗口,把除了“Option Explicit”之外的代码都删除。在“对象框”中选择“AddinInstance”,“过程/事件框”中选择“OnConnection”。 下载 (264.81 KB) 2008-7-24 01:35 即出现 - Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
- End Sub
复制代码 在该过程中写入“Set gExcelApp = Application” - Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
- Set gExcelApp = Application
- End Sub
复制代码 定义用来响应按钮事件的类,以便添加工具栏过程中使用。
添加一个类“ButtonEvent”,在类中定义一个响应事件的变量。 - Public WithEvents Button As Office.CommandBarButton
复制代码 如果事先知道有多少个按钮的话,可以用类数组。为了更好的扩展性,这里用一个集合类“ButtonEventCol”来管理“ButtonEvent”类。 添加一个类“ButtonEventCol”,“ButtonEventCol”类的代码: -
- Option Explicit
- Private mCol As Collection '集合,用来存放“ButtonEvent”类。
- Private Sub Class_Initialize()
- Set mCol = New Collection '类实例化时实例化集合
- End Sub
- Private Sub Class_Terminate()
- Set mCol = Nothing '类销毁时销毁集合
- End Sub
- Public Sub Add(ByRef Button As Office.CommandBarButton) '添加按钮
-
- Dim objNew As New ButtonEvent '创建“ButtonEvent”类新实例
-
- Set objNew.Button = Button '连接事件
- mCol.Add objNew '将类添加到集合中,只要“ButtonEventCol”类生存期未完,所有添加的“ButtonEvent”类就一直生存。
- Set objNew = Nothing
- End Sub
复制代码 创建工具栏和按钮
双击设计器“Connect”,按“F7”进入代码编辑窗口。刚才我们的代码 - Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
- Set gExcelApp = Application
- End Sub
复制代码 已经在 OnConnection 事件中连接 Excel 对象。我们就用“gExcelApp”这个对象变量来创建工具栏和按钮,并与响应事件的类连接。 - '定义一个变量用于存放工具栏对象。
- Private mMyBar As Office.CommandBar
复制代码 - '定义一个变量用于存放“ButtonEventCol”类实例
- Private mButtonEventCol As New ButtonEventCol
复制代码 假设我们在Excel创建一个名叫“外接程序”的工具栏,工具栏中有二个按钮,分别为:“新建工作簿”、“关闭工作簿”。 在“AddinInstance_OnStartupComplete”(对象已加载完毕)事件中创建工具栏、按钮,并把按钮和响应事件的类连接,代码如下: -
- Private Sub AddinInstance_OnStartupComplete(custom() As Variant)
- Dim MyControl As Office.CommandBarButton
- Set mMyBar = gExcelApp.CommandBars.Add(Name:="外接程序", Position:=msoBarFloating, Temporary:=True)
- mMyBar.Visible = True
- Set MyControl = mMyBar.Controls.Add(Type:=msoControlButton, Temporary:=True)
- With MyControl
- .BeginGroup = False
- .Caption = "新建工作簿"
- .Enabled = True
- .Visible = True
- .FaceId = 18
- .Style = msoButtonIconAndCaption
- .Parameter = "New" 'Parameter 属性是类在响应按钮事件时区分按钮的标记。
- '如果用 Tag 属性来区分的话,会发生二次事件。不知道为什么。
- End With
- mButtonEventCol.Add MyControl
- Set MyControl = mMyBar.Controls.Add(Type:=msoControlButton, Temporary:=True)
- With MyControl
- .BeginGroup = False
- .Caption = "关闭工作簿"
- .Enabled = True
- .Visible = True
- .FaceId = 1088
- .Style = msoButtonIconAndCaption
- .Parameter = "Close"
- End With
- mButtonEventCol.Add MyControl
- Set MyControl = Nothing
- End Sub
复制代码
关闭 Excel 时执行
- Private Sub AddinInstance_OnBeginShutdown(custom() As Variant)
- Set mButtonEventCol = Nothing
- '关闭 Excel 时释放类。
- '因为工具栏、按钮在创建的时候已经把 Temporary 属性设为 True 了,关闭 Excel 自动删除。
- '所以这里不用编写删除工具栏的代码。
- End Sub
复制代码 [ 本帖最后由 Coming 于 2008-7-24 01:44 编辑 ] |