none
Using .OnAction to reference a Word macro RRS feed

  • Question

  • I have a macro enabled (.docm) Word 2007 document that I’m using to develop a Word Add in.

     

    The VBA in the Word document uses an Auto Open macro to install a Control Bar (menu) in the Add-Ins group. This is what the code looks like:

    Set cbcNewMenu = cbMenuBar.Controls.Add _

                            (msoControlPopup, , , idHelp)

    cbcNewMenu.Caption = p_sMenuName

    With cbcNewMenu.Controls.Add(Type:=msoControlButton)

                        .Caption = "ConvertTable"

                        .OnAction = "mod100Procs.UpdateTable"

       

    End With

     

    The macro is in a separate module named mod100Procs and is declared like this:

     

    Public Sub UpdateTable()

    .

    .

    .

    End Sub

     

    The program installs the menu and the menu item: “Convert Table” works if the active document is the .docm document where the VBA code lives. When I try to access the menu while another document is active I get and error message the macro cannot be found.

     

    I tried changing the .OnAction to read like this:

    .OnAction = “’” & ThisDocument.Name & "’!mod100Procs.UpdateTable"

    To fully qualify it but that doesn’t work.

     

    I don’t have this problem in Excel. What am I missing?

     

    Wednesday, October 26, 2011 12:35 AM

Answers

  • In Word, VBA code is only available

    • from within the document that contains the code, or
    • from within a document based on the template that contains the code, or
    • if the code is in a global template (add-in)

    So you'll have to save your document as a macro-enabled template (.dotm), then load that template as an add-in.


    Regards, Hans Vogelaar
    Wednesday, October 26, 2011 5:28 AM

All replies

  • I'm using Word version 2007.

    I created a new command bar control that calls a macro like this:
    With cbcNewMenu.Controls.Add(Type:=msoControlButton)
    .Caption = "ConvertTable"
    .OnAction = "mod100Procs.UpdateTable"

    End With

    The macro only works when document that contains the VBA code is the active document. If another document is active and I select the command bar I get a "The macro cannot be found... message.

    I've tried this:

    With cbcNewMenu.Controls.Add(Type:=msoControlButton)
    .Caption = "ConvertTable"
    .OnAction = "'" & Thisdocument.name & "'!mod100Procs.UpdateTable"

    End With

    but that doesn't work. What am I missing?
    Edit/Delete Message
    Tuesday, October 25, 2011 11:58 PM
  • In Word, VBA code is only available

    • from within the document that contains the code, or
    • from within a document based on the template that contains the code, or
    • if the code is in a global template (add-in)

    So you'll have to save your document as a macro-enabled template (.dotm), then load that template as an add-in.


    Regards, Hans Vogelaar
    Wednesday, October 26, 2011 5:28 AM
  • In Word, VBA code is only available

    • from within the document that contains the code, or
    • from within a document based on the template that contains the code, or
    • if the code is in a global template (add-in)

    So you'll have to save your document as a macro-enabled template (.dotm), then load that template as an add-in.


    Regards, Hans Vogelaar
    Wednesday, October 26, 2011 5:28 AM
  • Hans,

    Thanks I'll give that a try.

    Regards,

    Don Buchanan

    Wednesday, October 26, 2011 3:47 PM