Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, and 2016. 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: Automatically Running a Macro.

Automatically Running a Macro

Written by Allen Wyatt (last updated June 1, 2021)
This tip applies to Word 2007, 2010, 2013, and 2016


3

You know that you can create macros within Word that allow you to automate many different functions. You may not know, however, that you can create macros that perform tasks without any intervention on your part. For instance, you can create a macro that Word will run automatically whenever you create a document; whenever you use the New command, this macro will run.

These special macros that automatically run at predefined times are identified by special names. Otherwise, there is nothing different between these macros and any other you may write. Here are the names you can give macros so that they will run automatically.

Macro Name When It Runs
AutoNew Whenever you create a new document
AutoClose Whenever you close a document
AutoExec Whenever you start Word
AutoExit Whenever you exit the program
AutoOpen Whenever you open a document

Remember that these macros, in order to be used effectively, must be saved within a DOCM document. (That is, within a document that is "macro enabled.") They cannot be saved in a DOCX document, as such documents cannot contain macros at all.

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 (9388) applies to Microsoft Word 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Word here: Automatically Running a Macro.

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

Using the Equation Editor for Fractions

How to use the Equation Editor when creating formulae in Word documents.

Discover More

Default Cell Movement when Deleting

Delete a cell or a range of cells, and Excel needs to figure out how to rearrange the void left by the deletion. You can ...

Discover More

Executing a Macro After Printing is Done

You can modify Excel's BeforePrint event handler to change how the printing process occurs. Unfortunately, though, Excel ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Determining the Size of a File

When processing a document using a macro, you may need to know the precise size of a particular file. The way you figure ...

Discover More

Creating a String

Need to use a macro to create a text string? One easy way to do it is to use the String function, described in this tip.

Discover More

Determining the Day of the Year

Need to find out the day of the year for a particular date? It's easy to do if you are using a macro. All you need to do ...

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 one less than 9?

2022-12-19 13:19:30

tim mc

Here's a tricky one. I would like to create a macro that executes after a file is selected in the File>Open dialog but before MS Word has actually opened/locked the document (want to shell out and run an app to an external version control commend on the file - a Get Latest type operation).

I believe the autoopen event would be too late for this. I want to avoid a kluge of creating a file flag, dynamically creating a batch file, closing word and chaining to new instance of word once the version control operation has been completed.


2018-10-18 01:54:18

Ken Gosse

I just want to thank you for many or your tips that I've used over a number of years to customize Word for my needs at home. My projects are not collaborative, so I don't worry about customizations to the default template (which I've learned to save as a copy in the default template folder as well as in my code document folders in a locations not affected by Windows or Office updates). Today, your page "Displaying the Navigation Pane when Opening a Document" showed me [CommandBars("Navigation").Visible]. I usually leave the Navigation panel open and know that ctrl-F will open it, too. I was looking for a way to automatically adjust the size when I open a document. Using your code, I discovered that [.Width] lets me do that, and I tested various widths to find what works best. Then I found your "Automatically Running a Macro" and added an AutoExec sub to adjust the panel width whenever I open a Word doc. (I do all my work in DOCM and, as necessary, save as a DOCX, such as when I submit poetry to online sites). Since I often need to review the sources of my code, I (almost) always include a comment with the web link for tips such as yours.
By the way, some of your tips port directly into my Access database codes, others send me in the right direction for Access. My Access macros inlcude thousands of lines of code. Site like yours offer invaluable help. I'm already subscribed to your newsletter, too.


2017-09-20 14:07:47

Bob Mathews

Surely there's something about this that I'm totally missing. The way I read the article, these 5 macros are macros I can create, and assign one of the 5 names to it, depending on when I want it to run. My Normal.dotm already has an AutoExec though, and it's locked. What's up?


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.