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

Counting Unique Values with Functions

Using Excel to maintain lists of information is not unusual. When working with the list you may need to determine how ...

Discover More

Selecting a Window

When you get a lot of windows open on your screen, it can sometimes be difficult to locate the one with which you need to ...

Discover More

Keeping Words in the Custom Dictionary

The spell checker that is part of Word allows you to create and use custom dictionaries to expand how the checker does ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Determining if Overtype Mode is Active

Your macro may need to determine if the user has overtype mode turned on. You can find out the overtype status easily by ...

Discover More

Determining if Caps Lock is On

If your macro needs to determine the status of the Caps Lock key, you need the code in this tip. Just use the Information ...

Discover More

Determining if Num Lock is On

Need to know if the Num Lock key is on or off? You can use a short bit of macro code to figure out the state of the key.

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 7?

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.