Implements IDTExtensibility2
'Global object references
Public appHostApp As Word.Application
Private WithEvents cbbButton As Office.CommandBarButton
Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
End Sub
Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
End Sub
Private Sub IDTExtensibility2_OnConnection(ByVal _
Application As Object, ByVal ConnectMode As _
AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst _
As Object, custom() As Variant)
' 存储启动引用
Set appHostApp = Application
' 添加命令条
Set cbbButton = CreateBar()
End Sub
Private Sub IDTExtensibility2_OnDisconnection(ByVal _
RemoveMode As AddInDesignerObjects.ext_DisconnectMode, _
custom() As Variant)
RemoveToolbar
' 移除要关闭的引用
Set appHostApp = Nothing
Set cbbButton = Nothing
End Sub
Public Function CreateBar() As Office.CommandBarButton
' 指定命令条
Dim cbcMyBar As Office.CommandBar
Dim btnMyButton As Office.CommandBarButton
On Error GoTo CreateBar_Err
Set cbcMyBar = appHostApp.CommandBars.Add(Name:="GreetingBar")
' 指定命令条按钮
Set btnMyButton = cbcMyBar.Controls.Add(Type:=msoControlButton, _
Parameter:="Greetings")
With btnMyButton
.Style = msoButtonCaption
.BeginGroup = True
.Caption = "&Greetings"
.ToolTipText = "Display Hello World Message"
.Width = "24"
End With
' 显示并返回命令条
cbcMyBar.Visible = True
Set CreateBar = btnMyButton
Exit Function
CreateBar_Err:
MsgBox Err.Number & vbCrLf & Err.Description
End Function
Private Function RemoveToolbar()
appHostApp.CommandBars("GreetingBar").Delete
End Function
Private Sub cbbButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
MsgBox ("Hello World!")
End Sub
Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
End Sub
以上是com加载项代码,此程序只能在打开的第一个word文档正常,打开多个后有加载工具栏但没有功能。请教! |