J
JB
I have a VB.NET Word add-in project that implements the IDTExtensibility2
interface. It's a simple toolbar with a few buttons that load up some .NET
forms that I built. When I launch the project, my toolbar appears within
Word and all is fine. I click the buttons, the events fire, and my forms
load. However, I noticed a problem when I open more than one Word document.
I launched Word, and typed some text in the main document, then went File ->
New -> Blank Document, and another Word window appeared. My toolbar appeared
in the second window as well, but when I click the buttons, they don't fire.
I have put breakpoints in the button click event to reassure this. Is there
something that I'm not doing right? I create the toolbar within the
OnConnection extensibility method. The code is below. I'm adding my click
event handlers dynamically when the button is created, but I didn't foresee
this as being a problem. Can anyone see what I might be doing wrong? Thankx.
Public Sub OnConnection(ByVal application As Object, ByVal
connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object,
ByRef custom As System.Array) Implements IDTExtensibility2.OnConnection
' Setup the application object.
InitWordObject(application)
Try
If Not _wordApp Is Nothing Then _whitehillBar =
MakeToolbar("WHToolbar")
If Not _whitehillBar Is Nothing Then
_xmlFileEdit = MakeEdit(True, "XML File: ",
_whitehillBar, "", 400, AddressOf _xmlFileEdit_Change)
_selectFile = MakeButton(False, _whitehillBar, "", 1661,
AddressOf _selectFile_Click)
_insertText = MakeButton(True, _whitehillBar, "XSL
Insert Wizard", 317, AddressOf _insertText_Click)
'_editItem = MakeButton(True, _whitehillBar, "Edit
Item", 3272, AddressOf _editItem_Click)
_tableWizard = MakeButton(True, _whitehillBar, "Table
Wizard", 333, AddressOf _tableWizard_Click)
_includeFile = MakeButton(True, _whitehillBar, "Include
File Wizard", 44, AddressOf _includeFile_Click)
_startConversion = MakeButton(True, _whitehillBar,
"Start Conversion", 1913, AddressOf _startConversion_Click)
' Read the configuration file.
ReadConfiguration()
End If
Catch ex As Exception
MessageBox.Show("There was a problem creating the Whitehill
toolbar.", _AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Function MakeButton(ByVal seperateFromPrevious As Boolean,
ByVal commandBar As CommandBar, ByVal caption As String, ByVal faceID As
Integer, ByVal clickHandler As _CommandBarButtonEvents_ClickEventHandler) As
CommandBarButton
Try
' Create the button.
Dim newButton As CommandBarButton
' Add the button to a commandbar.
newButton =
CType(commandBar.Controls.Add(MsoControlType.msoControlButton),
CommandBarButton)
' Start a seperator.
newButton.BeginGroup = seperateFromPrevious
' Set the caption of the button.
newButton.Caption = caption
' Set the Icon.
newButton.FaceId = faceID
' Add an event handler for the button.
AddHandler newButton.Click, clickHandler
' Return the button we've created.
Return newButton
Catch ex As System.Exception
Return Nothing
End Try
End Function
interface. It's a simple toolbar with a few buttons that load up some .NET
forms that I built. When I launch the project, my toolbar appears within
Word and all is fine. I click the buttons, the events fire, and my forms
load. However, I noticed a problem when I open more than one Word document.
I launched Word, and typed some text in the main document, then went File ->
New -> Blank Document, and another Word window appeared. My toolbar appeared
in the second window as well, but when I click the buttons, they don't fire.
I have put breakpoints in the button click event to reassure this. Is there
something that I'm not doing right? I create the toolbar within the
OnConnection extensibility method. The code is below. I'm adding my click
event handlers dynamically when the button is created, but I didn't foresee
this as being a problem. Can anyone see what I might be doing wrong? Thankx.
Public Sub OnConnection(ByVal application As Object, ByVal
connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object,
ByRef custom As System.Array) Implements IDTExtensibility2.OnConnection
' Setup the application object.
InitWordObject(application)
Try
If Not _wordApp Is Nothing Then _whitehillBar =
MakeToolbar("WHToolbar")
If Not _whitehillBar Is Nothing Then
_xmlFileEdit = MakeEdit(True, "XML File: ",
_whitehillBar, "", 400, AddressOf _xmlFileEdit_Change)
_selectFile = MakeButton(False, _whitehillBar, "", 1661,
AddressOf _selectFile_Click)
_insertText = MakeButton(True, _whitehillBar, "XSL
Insert Wizard", 317, AddressOf _insertText_Click)
'_editItem = MakeButton(True, _whitehillBar, "Edit
Item", 3272, AddressOf _editItem_Click)
_tableWizard = MakeButton(True, _whitehillBar, "Table
Wizard", 333, AddressOf _tableWizard_Click)
_includeFile = MakeButton(True, _whitehillBar, "Include
File Wizard", 44, AddressOf _includeFile_Click)
_startConversion = MakeButton(True, _whitehillBar,
"Start Conversion", 1913, AddressOf _startConversion_Click)
' Read the configuration file.
ReadConfiguration()
End If
Catch ex As Exception
MessageBox.Show("There was a problem creating the Whitehill
toolbar.", _AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Function MakeButton(ByVal seperateFromPrevious As Boolean,
ByVal commandBar As CommandBar, ByVal caption As String, ByVal faceID As
Integer, ByVal clickHandler As _CommandBarButtonEvents_ClickEventHandler) As
CommandBarButton
Try
' Create the button.
Dim newButton As CommandBarButton
' Add the button to a commandbar.
newButton =
CType(commandBar.Controls.Add(MsoControlType.msoControlButton),
CommandBarButton)
' Start a seperator.
newButton.BeginGroup = seperateFromPrevious
' Set the caption of the button.
newButton.Caption = caption
' Set the Icon.
newButton.FaceId = faceID
' Add an event handler for the button.
AddHandler newButton.Click, clickHandler
' Return the button we've created.
Return newButton
Catch ex As System.Exception
Return Nothing
End Try
End Function