Share via

Using .OnAction to reference a Word macro

Anonymous
2011-10-26T01:32:23+00:00

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?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2011-10-26T05:30:01+00:00

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.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2011-11-01T00:24:45+00:00

    Thank you that works.

    Was this answer helpful?

    0 comments No comments