Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default 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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default 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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default 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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Office VBA Automation Specialist salooha Excel Discussion (Misc queries) 4 August 15th 05 08:11 PM
MS Office VBA Automation Specialist salooha Excel Discussion (Misc queries) 0 August 15th 05 07:57 PM
Custom Menu return to Excel Menu upon Closing VetcalcReport Excel Programming 2 August 2nd 04 02:59 PM
asp.net Office Automation - in or out of process David Laub Excel Programming 4 April 27th 04 10:17 PM
Office automation application which will work with any version of office C#.NET Silly Programmer Excel Programming 1 February 21st 04 10:54 AM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"