ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Office Automation - Capturing Custom Menu Clicks (https://www.excelbanter.com/excel-programming/331841-office-automation-capturing-custom-menu-clicks.html)

luai7

Office Automation - Capturing Custom Menu Clicks
 
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).Capti on="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("Fi le").Controls.Add( _

Microsoft.Office.Core.MsoControlType.msoControlBut ton, _
Temporary:=False, Befo=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").Contro ls("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").Contro ls("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


keepITcool

Office Automation - Capturing Custom Menu Clicks
 

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 :

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).Capti on="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("Fi le").Controls.Add(
_ Microsoft.Office.Core.MsoControlType.msoControlBut ton, _
Temporary:=False, Befo=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").Contro ls("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").Contro ls("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


luai7

Office Automation - Capturing Custom Menu Clicks
 
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.

"keepITcool" wrote:


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 :

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).Capti on="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("Fi le").Controls.Add(
_ Microsoft.Office.Core.MsoControlType.msoControlBut ton, _
Temporary:=False, Befo=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").Contro ls("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").Contro ls("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



keepITcool

Office Automation - Capturing Custom Menu Clicks
 

im a VBA hack and i dont .NET so dont know .NET specifics,
apparently you do need events handlers

see
How To Use Automation to Create Office Command Bars and Controls with
Visual Basic .NET
http://support.microsoft.com/?kbid=303017



--
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.

"keepITcool" wrote:


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 :

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).Capti on="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("Fi le").Controls.Ad
d( _ Microsoft.Office.Core.MsoControlType.msoControlBut ton, _
Temporary:=False,
Befo=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").Contro ls("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").Contro ls("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




All times are GMT +1. The time now is 12:09 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com