Office Automation - Capturing Custom Menu Clicks

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
normally you'd
set the OnAction property of the commandbarbutton.
to point to a public procedure in a dll or vba project


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


luai7 wrote :
 
Thanks for your quick reply.

What is the syntax? Where should I place my .Net DLL?
Do you know where I can find samples?

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top