Hi Jerry,
I have tried to check the ConnectMode and RemoveMode but it is strange that
I still can not reproduce the problem.
The below is the code.
Because my code is for demo purpose, it is not a valid software, so I did
not make it very formal, I am sorry for that.
For cbb issue, in .NET, the GC will try to release the managed reference.
I think you need to pay attention to two points.
1. Add the Toolbar temporarily
2. Call the Delete method when the Addin is going to Disconnection/Unload.
Imports Microsoft.Office.Core
imports Extensibility
imports System.Runtime.InteropServices
Imports Word = Microsoft.Office.Interop.Word
Imports Office = Microsoft.Office.Core
#Region " Read me for Add-in installation and setup information. "
' When run, the Add-in wizard prepared the registry for the Add-in.
' At a later time, if the Add-in becomes unavailable for reasons such as:
' 1) You moved this project to a computer other than which is was
originally created on.
' 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
' 3) Registry corruption.
' you will need to re-register the Add-in by building the
WordAddin_CommandBarsSetup project
' by right clicking the project in the Solution Explorer, then choosing
install.
#End Region
<GuidAttribute("769FF42D-B7CA-45C3-8ACB-46EB3E54700C"),
ProgIdAttribute("WordAddin_CommandBars.Connect")> _
Public Class Connect
Implements Extensibility.IDTExtensibility2
Dim wdApp As Word.Application
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnBeginShutdown
Dim cmBar As Office.CommandBar
Dim cbb As Office.CommandBarButton
Debug.WriteLine("OnBeginShutdown")
Try
cmBar = wdApp.CommandBars("Test")
cbb = cmBar.FindControl(, , "TestTag", , )
Catch ex As Exception
Debug.WriteLine(ex.ToString())
End Try
If Not cbb Is Nothing Then
cbb.Delete()
End If
If Not cmBar Is Nothing Then
cmBar.Delete()
End If
Marshal.ReleaseComObject(cbb)
Marshal.ReleaseComObject(cmBar)
cbb = Nothing
cmBar = Nothing
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnAddInsUpdate
Debug.WriteLine("OnAddInsUpdate")
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnStartupComplete
Debug.WriteLine("OnStartupComplete")
Dim cmBar As Office.CommandBar
Dim cbb As Office.CommandBarButton
If MsgBox("Do you want Create Toolbar?", MsgBoxStyle.YesNo) =
MsgBoxResult.Yes Then
cmBar = wdApp.CommandBars.Add("Test", , , True)
cmBar.Visible = True
cmBar.Position = MsoBarPosition.msoBarTop
cbb =
cmBar.Controls.Add(Office.MsoControlType.msoControlButton, , , , True)
cbb.Caption = "TestBar"
cbb.FaceId = 17
cbb.Tag = "TestTag"
Debug.WriteLine("Create Toolbar Complete")
End If
Marshal.ReleaseComObject(cbb)
Marshal.ReleaseComObject(cmBar)
cbb = Nothing
cmBar = Nothing
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As
Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnDisconnection
Debug.WriteLine("OnDisconnection")
If Not RemoveMode = ext_DisconnectMode.ext_dm_HostShutdown Then
OnBeginShutdown(custom)
End If
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal connectMode
As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom
As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
wdApp = DirectCast(application, Word.Application)
Debug.WriteLine("OnConnection")
If Not connectMode = ext_ConnectMode.ext_cm_Startup Then
OnStartupComplete(custom)
End If
End Sub
End Class
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.