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 – 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.
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 – 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, I have experienced that 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.
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 a macro 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.
Macro – extract comments to new document
If a document contains many comments (inserted via Insert > Comment), you may wish to get an overview of all the comments in one place. Here you will find a macro that lets you extract all the comments to a new document.
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 a macro that lets 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 will only include insertions and deletions in the main body of the document (i.e. changes in headers, footers, footnotes and endnotes will not be included).
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.