VBA macros and code snippets for Microsoft Word
Below you will find VBA macros and code snippets related to Microsoft Word.
More VBA macros and code snippets will be added as time permits.
Some of the macros or code snippets may first have been created as part of my answers to users who have posted questions to Microsoft Word newsgroups. The versions included here may, however, be revised compared to my newsgroup answers.
Please note that all VBA macros, code snippets and other information are provided "as is" and without obligation.
How to install the macros?
Macro – Extract Tracked Changes to New Document
You may wish to get en overview of tracked changes (revisions) in a document. You can print the changes by selecting "List of Markup" in the "Print What" field in the Print dialog box.
However, you may want the overview in electronic form. Here you will find both a macro, an advanced Word add-in and a free Word add-in that let you extract the tracked changes to a new document. Note that only insertions and deletions will be included whereas other types of changes (formatting changes etc.) will be skipped. Also note that the macro and the free add-in will only include insertions and deletions found in the main body of the document whereas the advanced add-in will also include insertions and deletions found in headers, footers, footnotes and endnotes.
Macro – Extract Comments to New Document
If a document contains many comments (inserted via Review tab > New Comment), you may wish to get an overview of all the comments in one place. Here you will find both a macro and a free Word add-in that lets you extract all the comments to a new document.
Macro – Extract ACRONYMS to New Document
In some documents, you may use many acronyms
(i.e. words formed from the initial letters of multi-word names,
e.g. VBA for "Visual Basic for Applications"). It is helpful to include the
definition/full name the first time you mention an acronym. Alternatively,
you may want to create a list of all the acronyms and include the definitions
in the list. Here you will find both a macro and a free Word add-in that lets you extract all the acronyms
of a document to a table in a new document. The table has room for the definitions
and also shows the page number of the first page where each acronym is used. The
acronyms will be sorted alphabetically.
Add-in – Extract Bookmarks to New Document
A bookmark is used to assign a name to a position or an area in a document so it can be referred to from elsewhere, e.g. in cross-references or macro code. You can manually add bookmarks via Insert > Bookmark.
Word also automatically adds bookmarks in some situations, e.g. when generating a table of contents.
If a document contains many bookmarks, you may wish to get an overview of all the bookmarks and any contents enclosed in the bookmarks in one place. Here you will find link to a free Word add-in that lets you extract / export all the bookmark data to a new document.
Macro – Add Screen Tips to Text in Microsoft Word
Many users want to be able to make small screen tips in Word that work like the screen tips known from Websites,
i.e. small boxes with information that appear when you hover the mouse over a text like here. For example, such screen tips may be used to show definitions of terms.
Word has no functionality specifically for
that but you can make use of the hyperlink functionality and obtain something similar. You can create such "screen tips hyperlinks" manually.
However, this includes a number of steps: You must select text, add a bookmark with an appropriate name, create a hyperlink and enter the screen tip text, and
format the text.
Here you will find a set of macros that automate the process. One macro lets you add screen tips hyperlinks in a structured way
and another one lets you easily remove a screen tip hyperlink again if you wish.
Troubleshooting / macro – Word fails to show font name and other formatting values – shows blank fields
You may sometimes need to find out whether your text or part of it is formatted with the same font, font size, paragraph style or
other formatting properties. This can be as easy as selecting the text in question and checking which values Word shows for the properties
in question. But it may not always work as you want or expect.
This article explains the rules that apply to the way Word shows or does not show formatting information. You will also find suggested solutions plus a macro that can automatically perform the check for you, also when Word fails to do so.
Macro – Create or Edit AutoText via VBA
You may elsewhere find information telling that you can only create an AutoText via VBA if you first insert the desired content in a document.
Also, you may find information telling that you cannot change the content of an existing AutoText via VBA without first inserting the AutoText, then editing
the content and replacing the old AutoText with a new version. However, you can actually create an AutoText via VBA and set
the value to whatever string you want without first inserting the string in a document. Correspondingly, you can change the content of an existing
AutoText directly via VBA without first inserting the old version in a document. Please note that the methods described are only well suited for AutoTexts that consist of plain text only
and that do not exceed 255 characters.
Macro – Keep With Next if Colon at End of Paragraph
You may want any paragraph that ends with a colon to always be on the same page as the next paragraph. You can handle this by manually applying Keep with next to all such paragraphs. However,
there is an easier way. In this article, you
will find a macro that does the work for you – it applies Keep with next to all paragraphs in the active document that end with a colon.
Macro – Create List of Local Built-in Style Names
If you are creating a Word document, a template or an add-in that is going to be used on PCs with different
language versions of Microsoft Word installed, you need to pay attention to the fact that the names of the built-in styles
are language-specific. This article discusses some of the problems that may occur.
In addition, you can download a Word document that lets you create a list of the local built-in style names with the click of a button.
Macro – Print Without Margins Warning
Most printers cannot print to the edge of the paper. If you attempt to print a document with content that is outside the printable area of the page, a warning will tell you that there is a problem. You can choose to ignore the problem or you can cancel the print operation in order to fix the problem. There may be times when you want to suppress the warning and print the entire document despite of the margins problem. This article provides a couple of macros you can use for that purpose.
Macro – Copy Text and Retain Numbers When Pasting
You may often be in a situation where you need to copy part of a document to another place, e.g. an e-mail.
However, if your document includes automatic numbered text such as numbered headings, this may cause problems if the part you want to copy
does not include the first numbered item(s) in the document. In that case, the numbers in the original document and the text you paste will not match.
This article provides a macro solution that will create a copy of the text in which the automatic numbers have been converted to normal text so that the numbers are retained when pasted.
Troubleshooting – Toolbar Missing on Add-Ins Tab
If you are using Microsoft Word 2007 or 2010 and if you have created custom toolbars in earlier versions of Microsoft Word, those toolbars can be made available from the Add-Ins tab in Microsoft Word 2007 or 2010. In order to appear on the Add-Ins tab, the toolbars must be part of templates stored in Microsoft Word's Startup folder. However, I have experienced a few times, that a custom toolbar did not automatically appear in Microsoft Word 2007 or 2010. This article provides help on how you can make a missing custom toolbar visible.
Troubleshooting – Shortcut to Macro Does Not Work
You can assign shortcuts to macros either via the user interface or programmatically via VBA. This article provides a workaround if you encounter the problem that the shortcut you assign to a macro does not work.
Macro – Apply Uniform Table Borders to All Tables
Situation: You have a document with several tables. The tables have different borders or another border style than desired. You want all the tables to have uniform borders. You may have experienced that this can be rather time-consuming if you have to correct the tables one by one. Here you will find a macro that corrects the table borders of all table cells in all tables in one operation.
Top of page
Back to Macros...
ADD-INS Word add-ins ready for use - work smarter in Word