Written by Allen Wyatt (last updated June 1, 2021)
This tip applies to Word 2007, 2010, 2013, and 2016
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:
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.
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!
Your macro may need to determine if the user has overtype mode turned on. You can find out the overtype status easily by ...
Discover MoreIf your macro needs to determine the status of the Caps Lock key, you need the code in this tip. Just use the Information ...
Discover MoreNeed 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 MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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?
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments