+ Reply to Thread
Results 1 to 4 of 4

Office Automation - Capturing Custom Menu Clicks

  1. #1
    luai7
    Guest

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


  2. #2
    keepITcool
    Guest

    Re: 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).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


  3. #3
    luai7
    Guest

    Re: 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).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

    >


  4. #4
    keepITcool
    Guest

    Re: 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).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.Ad
    > > > d( _ 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

    > >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1