G
Guest
I am trying to automate MS-Word 2003 from my VB.Net client application using
the latest PIAs for Office2003.
My work also involves creating custom menus and toolbars in MS-Word and
capturing their click events at the VB.Net client application side.
I wrote a simple code (see below) based on code snippets from Microsoft and
other forums.
The problem that I am having is that when clicking my custom menu item the
corresponding
event is never fired !!! However it gets fired for built-in Word menu items
!!!
Is there any way to do that without using Add-Ins or VSTO, just simple
automation ??
Please help...........
Here is the VB.Net (2003) code:
Imports Office = Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word
.......
Public WithEvents WordApp As New Word.ApplicationClass
Public WithEvents cbMenuItem As Microsoft.Office.Core.CommandBarButton
Public Function AddWordMenus()
Dim SaveAsPosition As Integer
Dim i As Integer
RemoveWordMenus()
' Find the position for the Save As menu
For i = 1 To WordApp.CommandBars.Item("File").Controls.Count
If WordApp.CommandBars.Item("File").Controls(i).Caption="Save
&As..." _
Then
SaveAsPosition = i
Exit For
End If
Next i
' Create New Menu object and add it to the built-in Menu Bar.
cbMenuItem=DirectCast(WordApp.CommandBars.Item("File").Controls.Add( _
Microsoft.Office.Core.MsoControlType.msoControlButton, _
Temporary:=False, Before:=SaveAsPosition + 1), _
Microsoft.Office.Core.CommandBarButton)
cbMenuItem.Caption = "MyCustomMenu"
cbMenuItem.Visible = True
AddHandler cbMenuItem.Click, AddressOf cbMenuItem_Click
' I also tried this but it did not work !!
' --- AddHandler
DirectCast(WordApp.CommandBars.Item("File").Controls("MyCustomMenu"),
Microsoft.Office.Core.CommandBarButton).Click, AddressOf cbMenuItem_Click
' Funny enough this one works (i.e. for the built in Word menu: Save
As)
' --- AddHandler
DirectCast(WordApp.CommandBars.Item("File").Controls("Save &As..."),
Microsoft.Office.Core.CommandBarButton).Click, AddressOf cbMenuItem_Click
End Function
Public Sub cbMenuItem_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
' This event never gets fired for custom menus !!!!
MsgBox("This is my event handler!", MsgBoxStyle.Information)
End Sub
the latest PIAs for Office2003.
My work also involves creating custom menus and toolbars in MS-Word and
capturing their click events at the VB.Net client application side.
I wrote a simple code (see below) based on code snippets from Microsoft and
other forums.
The problem that I am having is that when clicking my custom menu item the
corresponding
event is never fired !!! However it gets fired for built-in Word menu items
!!!

Is there any way to do that without using Add-Ins or VSTO, just simple
automation ??
Please help...........
Here is the VB.Net (2003) code:
Imports Office = Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word
.......
Public WithEvents WordApp As New Word.ApplicationClass
Public WithEvents cbMenuItem As Microsoft.Office.Core.CommandBarButton
Public Function AddWordMenus()
Dim SaveAsPosition As Integer
Dim i As Integer
RemoveWordMenus()
' Find the position for the Save As menu
For i = 1 To WordApp.CommandBars.Item("File").Controls.Count
If WordApp.CommandBars.Item("File").Controls(i).Caption="Save
&As..." _
Then
SaveAsPosition = i
Exit For
End If
Next i
' Create New Menu object and add it to the built-in Menu Bar.
cbMenuItem=DirectCast(WordApp.CommandBars.Item("File").Controls.Add( _
Microsoft.Office.Core.MsoControlType.msoControlButton, _
Temporary:=False, Before:=SaveAsPosition + 1), _
Microsoft.Office.Core.CommandBarButton)
cbMenuItem.Caption = "MyCustomMenu"
cbMenuItem.Visible = True
AddHandler cbMenuItem.Click, AddressOf cbMenuItem_Click
' I also tried this but it did not work !!
' --- AddHandler
DirectCast(WordApp.CommandBars.Item("File").Controls("MyCustomMenu"),
Microsoft.Office.Core.CommandBarButton).Click, AddressOf cbMenuItem_Click
' Funny enough this one works (i.e. for the built in Word menu: Save
As)
' --- AddHandler
DirectCast(WordApp.CommandBars.Item("File").Controls("Save &As..."),
Microsoft.Office.Core.CommandBarButton).Click, AddressOf cbMenuItem_Click
End Function
Public Sub cbMenuItem_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
' This event never gets fired for custom menus !!!!
MsgBox("This is my event handler!", MsgBoxStyle.Information)
End Sub