A
Ashok Kumar K
Hello,
I have written a COM addin to Microsoft Word 2000 using VC++. In the
OnConnection method of the _IDTExtensibility2 interface, I am creating a
toolbar in which I have some CommandBarButton and a CommandBarComboBox. I am
advising for events sink using the _CommandBarButtonEvents and the
_CommandBarComboBoxEvents interfaces.
When launch word, the toolbar gets created and everything works. I am able
to get the events for the button as well as the combobox change event. When
I open another word document (i.e. a second word document), the toolbar is
displayed in this window, the events for the button click comes, but not the
combo box change event. When I go back to the first document while the
second document is open, everything still works normal, I get both the
events. I noticed this all the subsequent windows as well. Only in the first
document window, the combo box change event comes and the rest of the window
the change event does not fire at all. Whereas the button event comes in all
the windows.
I have tried the same in VB as well. The result is the same. Why does word
behave like this and how do I get the Change event in all the
windows/documents?
Following the sample code that I have written in VB to simulate the problem.
------------------------------------------
'TestAddin.cls
Option Explicit
Private oApp As Word.Application
Private oTBHandler As ToolbarHandler
Implements IDTExtensibility2
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 oApp = Application
Set oTBHandler = New ToolbarHandler
oTBHandler.CreateToolbar oApp
End Sub
Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As
AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
oTBHandler.DeleteToolbar
End Sub
Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
End Sub
-----------------------------------------------------------------
'ToolbarHandler.cls
Option Explicit
Private oApp As Word.Application
Private WithEvents oCmd As Office.CommandBarButton
Private WithEvents ocmb As Office.CommandBarComboBox
Public Sub CreateToolbar(App As Word.Application)
Dim oTB As Office.CommandBar
Set oApp = App
Set oTB = App.CommandBars.Add("Test Addin")
oTB.Visible = True
Set oCmd = oTB.Controls.Add(msoControlButton)
oCmd.Caption = "click me"
oCmd.Tag = "Test_button"
oCmd.Style = msoButtonCaption
Set ocmb = oTB.Controls.Add(msoControlComboBox)
ocmb.Tag = "Test_Combo"
ocmb.AddItem "one"
ocmb.AddItem "two"
ocmb.AddItem "three"
End Sub
Private Sub ocmb_Change(ByVal Ctrl As Office.CommandBarComboBox)
Dim c As CommandBarComboBox
Set c = Ctrl
MsgBox c.Text
End Sub
Private Sub oCmd_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault
As Boolean)
MsgBox "clicked"
End Sub
Public Sub DeleteToolbar()
oApp.CommandBars("Test Addin").Delete
End Sub
I have written a COM addin to Microsoft Word 2000 using VC++. In the
OnConnection method of the _IDTExtensibility2 interface, I am creating a
toolbar in which I have some CommandBarButton and a CommandBarComboBox. I am
advising for events sink using the _CommandBarButtonEvents and the
_CommandBarComboBoxEvents interfaces.
When launch word, the toolbar gets created and everything works. I am able
to get the events for the button as well as the combobox change event. When
I open another word document (i.e. a second word document), the toolbar is
displayed in this window, the events for the button click comes, but not the
combo box change event. When I go back to the first document while the
second document is open, everything still works normal, I get both the
events. I noticed this all the subsequent windows as well. Only in the first
document window, the combo box change event comes and the rest of the window
the change event does not fire at all. Whereas the button event comes in all
the windows.
I have tried the same in VB as well. The result is the same. Why does word
behave like this and how do I get the Change event in all the
windows/documents?
Following the sample code that I have written in VB to simulate the problem.
------------------------------------------
'TestAddin.cls
Option Explicit
Private oApp As Word.Application
Private oTBHandler As ToolbarHandler
Implements IDTExtensibility2
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 oApp = Application
Set oTBHandler = New ToolbarHandler
oTBHandler.CreateToolbar oApp
End Sub
Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As
AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
oTBHandler.DeleteToolbar
End Sub
Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
End Sub
-----------------------------------------------------------------
'ToolbarHandler.cls
Option Explicit
Private oApp As Word.Application
Private WithEvents oCmd As Office.CommandBarButton
Private WithEvents ocmb As Office.CommandBarComboBox
Public Sub CreateToolbar(App As Word.Application)
Dim oTB As Office.CommandBar
Set oApp = App
Set oTB = App.CommandBars.Add("Test Addin")
oTB.Visible = True
Set oCmd = oTB.Controls.Add(msoControlButton)
oCmd.Caption = "click me"
oCmd.Tag = "Test_button"
oCmd.Style = msoButtonCaption
Set ocmb = oTB.Controls.Add(msoControlComboBox)
ocmb.Tag = "Test_Combo"
ocmb.AddItem "one"
ocmb.AddItem "two"
ocmb.AddItem "three"
End Sub
Private Sub ocmb_Change(ByVal Ctrl As Office.CommandBarComboBox)
Dim c As CommandBarComboBox
Set c = Ctrl
MsgBox c.Text
End Sub
Private Sub oCmd_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault
As Boolean)
MsgBox "clicked"
End Sub
Public Sub DeleteToolbar()
oApp.CommandBars("Test Addin").Delete
End Sub