Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Using Call to Run VBA Macros.

Using Call to Run VBA Macros

Written by Allen Wyatt (last updated May 25, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


1

While it is possible to run a VBA macro from another macro by using the approach presented in the previous tip, there are times when such an approach just won't do the job. Instead, you need to look for a solution that allows a fail-safe method of running a macro. One approach is to use the Call command in VBA. Before you can use the command, however, you need to add a project reference to the calling project. (Projects are how VBA refers to the collection of VBA modules, forms, etc. in a template. For example, VBA refers to the Normal template as the "Normal" project.) References between projects provide an unambiguous defined link between a calling project and the project to be called.

For instance, let's say that the MyMacro macro (the one you wanted to run) was a part of the CoolDoc template. To use Call properly in VBA, you need to add a reference to the CoolDoc.dotm project to the macro that will call MyMacro. Unfortunately, this is a bit easier said than done because of a potential "gottcha" in VBA: Other than the "Normal" project, VBA gives all user-created template projects the default project name of "TemplateProject." This can create problems since having a unique project name is best when wishing to be specific about the macro to run. To give CoolDoc.dotm a unique project name, follow these steps:

  1. Make sure the CoolDoc.dotm template is open.
  2. Display the VBA editor by pressing Alt+F11.
  3. In the Project window at the upper-left of the VBA editor, right-click on the folder for the template. Since CoolDoc is a template, the folder is named TemplateProject (CoolDoc). Word displays a Context menu.
  4. Choose the TemplateProject Properties option from the Context menu. The Properties dialog box is displayed.
  5. In the Project Name field, enter a suitable name for your project. (CoolDoc would be a fine name, as long as it is unique.)
  6. Click on OK. The dialog box disappears and the project is renamed to CoolDoc.

If you expand all the objects under CoolDoc, you should see a module or two in the project. (Remember; VBA macros are contained in modules.) For the sake of this discussion, let's assume that MyMacro (the one you want to run) is contained within the HotStuff module of the CoolDoc project. (All these object names will be used shortly. At this point, you just need to note the names used for your project and the module with the macro.)

Now you are ready to actually add the CoolDoc project reference to the project from which the macro will be called. In this instance, let's assume you will be calling it from a macro in the Normal project. You can add the reference by following these steps (assuming that the VBA editor is still open):

  1. In the Project window at the upper-left of the VBA editor, select the Normal folder.
  2. Choose References from the Tools menu. The References dialog box is displayed. (See Figure 1.)
  3. Figure 1. The References dialog box.

  4. In the list of available references, find CoolDoc.
  5. Make sure the check box to the left of the CoolDoc option is selected.
  6. Click on OK.

Finally, you are ready to add the Call statement to the macro from which you want to call MyMacro. The command line would appear as follows:

Call CoolDoc.HotStuff.MyMacro

This command line causes VBA to run the desired macro, without any chance of ambiguity. There is something important to note, however. Putting a reference to the CoolDoc project in Normal makes Normal load a copy of CoolDoc every time Word is started—even if there are no documents open that are based on the CoolDoc.dotm template.

This loading of CoolDoc raises and obvious question: What is the point, in VBA, of putting MyMacro in CoolDoc.dotm since it won't save load time or resource usage? The short answer is that there isn't any point, other than CoolDoc.dotm possibly being a more obvious place to put code that only relates to things in CoolDoc.dotm. (This whole topic of sensible project design is far too big a subject to discuss in this single tip.)

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11412) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Using Call to Run VBA Macros.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Understanding Outlining

Outlining, a feature built into Excel, can be a great way to help organize large amounts of data. This tip provides an ...

Discover More

Putting Something in Every Cell of a Table

Need to make sure that all the cells of a table have something in them? It's easy to do with a handy little macro.

Discover More

Selecting Objects

Windows uses a graphical user interface that requires the manipulation of objects that appear on the screen. ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Understanding the For ... Next Structure

Spend any time creating Word macros, and sooner or later you will need to repeat some of your programming code a certain ...

Discover More

Changing ToolTips for a Macro Button

Want to change the ToolTip that appears when you hover the mouse pointer over a tool on the Quick Access Toolbar? Here's ...

Discover More

Determining the Number of Paragraphs in a Document

When using a macro to process a document in some way, you often need to know the number of paragraphs in the document. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 3?

2023-08-16 16:58:00

Timothy Barton

Thanks for the advice. I'm looking to do this the other way round: I want a macro in a template to call a macro in normal.dotm.

If I run the main macro from normal.dotm, it works. An example of one of the lines is this:

------ Call FindReplaceAnywhere("Å", ChrW(448))

From what I've read, to run this line from a template, I'd change it to this:

------ Application.Run "Normal.FindReplace.FindReplaceAnywhere", "Å", ChrW(448))

But then I get run-time error 438: Object doesn't support this method or variable. Any idea why?

For what it's worth, the FindReplaceAnywhere macro is designed to take two arguments:

------ Public Sub FindReplaceAnywhere(pFindTxt As String, pReplaceTxt As String)


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.