Need to do some serious calculating, formatting, or converting in your document? You can use macros to perform many actions in Word that will make your document editing process less time consuming. Learn how to create, use, and easily access the macros you need with the following articles.
Tips, Tricks, and Answers
The following articles are available for the 'Macros' topic. Click the article''s title (shown in bold) to see the associated article.
Adding a Macro to a Toolbar
One of the easiest ways to quickly access a macro is to assign it to a toolbar button. How you make the assignment depends on the version of Word you are using.
Adding Smart Quotes through Macro Text
When text is added to your document by a macro, and that text includes quotes or apostrophes, Word won't change the quotes or apostrophes to "smart quotes." This tip explains why and shows what you can do about this predicament.
Aligning a Paragraph in a Macro
If you are applying formatting from within a macro, you may want to change the alignment of various paragraphs. Here's how you can do it by adjusting the Alignment property.
Assigning a Macro to a Button in Your Text
One way you can access macros is through the use of a button, added directly into the text of your document. This is done with the MacroButton field, described in this tip.
Automatically Inserting Brackets
Want a fast way to add brackets around a selected word? You can use this simple macro to add both brackets in a single step.
Automatically Inserting Tomorrow's Date
Do you routinely need to work with tomorrow's date? Why not create a template that automatically adds tomorrow's date to any point in the document you want. This tip shows how easy this can be.
Automatically Running a Macro
Word allows you to create macros that can run at special times, automatically. This tip explains five special macros that fall into this category.
Batch Template Changes
Changing the template associated with a couple of documents is easy, but what if a whole directory needs to be changed? These macros will do it very quickly.
Bypassing the Startup Macro
Word allows you to create a macro that is run automatically whenever the program is started. If you want to bypass the macro, you can do it from the Windows command prompt using the technique described in this tip.
Calculating a Future Date
Need to figure out a date a certain number of days, weeks, months, or years in the future? It's easy to do using the DateAdd function, described in this tip.
Changing between English Variants
What is the easiest way to switch between English spelling variants in a document? This tip examines a couple of ways you can handle the desired conversion.
Changing Directories in a Macro
When a macro works with files, it often has to change between different directories on your disk drive. This is done using the ChDir command, as described in this tip.
Changing Roman Numerals to Arabic
In some documents Roman numerals might be used quite a bit. If you ever want to change the Roman numerals to their Arabic equivalent, Word doesn't have a built-in function. You can create your own conversion macro, though, as discussed in this tip.
Changing ToolTips for a Macro Button
Want to change the ToolTip that appears when you hover the mouse over a button on a toolbar? It's a bit more involved than you may think, but a short macro can do the trick.
Changing What Is Pasted in a Dialog Box
When you record a macro, Word very literally records what you do. This includes filling in various settings in dialog boxes. How you change those settings is the subject of this tip. (Hint: Don't rely on the dialog box; change the command that results from the dialog box.)
Cleaning Up Text in a Macro
Need to remove extraneous characters from a text string? VBA makes it easy through the CleanString method, described in this tip.
Controlling Repagination in Macros
Want to turn off document repagination while your macro does its work? Here are two approaches you can use.
Controlling the Bold Text Attribute
When processing a document in a macro, you may need to make some of your text bold. It's easy to do using the Bold attribute, as described in this tip.
Converting Strings to Numbers
When creating macros, you often need to convert a text string that contains numbers into actual numeric values. You do this by using the Val function, described in this tip.
Converting Text to Uppercase in a Macro
Macros are often used to process documents. If part of the processing involves making text selections uppercase, Word provides two ways that the conversion can occur.
Counting the Instances of a Text String
Sometimes it is helpful to know how often a particular phrase appears within a document. If you need to know such a count, here's a handy macro you can use to get the desired info.
Creating a Command List
Want a list of all the commands available in Word? You can get one easily by following these steps.
Creating a Directory
Need to create a directory from within a macro? You can do it using a single command line, as detailed in this tip.
Creating a Document Font List
If you want a list of all the fonts used in a document, the answer isn't as simple as you may think. This tip uses macros that step through the document and compile the desired font list, but they take a while to run.
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.
Defining a Shortcut for a Macro
You can make running macros very easy if you assign a shortcut key to the macro. This tip demonstrates how easy it is to create these shortcut keys for running macros.
Detecting an Open Dialog Box
Macros can be used to perform all sorts of tasks within Word. Some tasks can even occur at whatever time interval you desire. If there is a dialog box open when your macro tries to run, however, it could cause problems for your code.
Determining a Random Value
If you need to determine a random value in a macro, you can do so using the Rnd function. This tip presents the syntax and usage of the function.
Determining an Integer Value
One of the math functions you can use in your macros is the Int function. It provides a way for you to derive an integer value from whatever original values you are working with.
Determining the Horizontal Position of the Insertion Point
Need to figure out how far the insertion point is from the left margin? You can do so by using this small macro that relies on the Information property.
Determining the Length of a String
Need to find out in a macro how long a particular text string is? You can figure it out by using the Len function, described in this tip.
Displaying the Document Title Right Away
One of the properties that Word maintains for a document is a title. If you want this title displayed on the title bar right after opening the document, you'll need the single-line macro described in this tip.
Editing Word's Built-in Commands
Want to configure Word to do just what you want it to? You can even go so far as to change the actual way in which Word performs its internal commands. This tip explains how you use the VBA editor to change those internal commands.
Extracting INCLUDEPICTURE File Names
If you use the INCLUDEPICTURE field to add images to your document, you may love the macro in this tip. It allows you to pull all the filenames used in the field, resulting in a handy image file list.
Finding and Changing Word's Internal Commands
If you know how to create macros, you can easily create entire replacements for Word's internal commands. Here's all you need to do.
Finding Long Lines
Word is very dynamic in how it "flows" text from one line to another and one page to another. In most cases we are willing to allow Word to do its job in this area. In some situations, however, you may need to know if a paragraph has flowed to a new line so that you can "force" it to fit all on one line. Making this determination is not as easy as you might like.
Finding Long Sentences
For certain types of writing, you may want to make sure that the sentences in your document do not exceed a certain limit. Word provides no tool to do this, but you can create your own tool for checking sentence length. This tip shows you how.
Forcing the Date to the Next Wednesday
Working with today's date in Word is easy. Trying to manipulate dates to come up with a future one can be an entirely different story. This tip provides a quick and easy macro that inserts the date of whatever next Wednesday happens to be.
Getting User Input in a Dialog Box
Want to grab some interactive input from a user in your macro? The best way to do that is with the InputBox function, described in this tip.
Highlight Words from a Word List
Do you need to highlight certain words in a document, and aren't quite sure how to go about it? Using the techniques described in this tip, you can create a word list document and then run a macro to highlight all the occurrences of the words in that word list.
Highlighting Every Thousandth Character
Not satisfied with the detail provided by the Word Count feature in Word? Perhaps you want to actually know where every one thousandth character occurs in your document. The only way to find out this information is to use a macro, like those detailed in this tip.
Inserting a Paragraph from within a Macro
Macros are often used to process documents, resulting in changes of one manner or another. If you need your macro to add paragraphs to the document, you'll appreciate this tip.
Inserting Text with a Macro
Need to have your macro insert a bit of text into your document? It's easy to do using the TypeText method.
Intelligible Names for Macros
The names you use for macros can affect what you see when you add those macros to a toolbar. This tip explains how you can change macro names on toolbars, as well as change the ToolTip that appears for a macro.
When configuring Word, you may want to add macros to either menus or toolbars. If you can't find your macros while doing the configuration, it could be due to the scenario described in this tip.
Listing Documents with Passwords
Do you need a list of documents that require a password or that require a particular password to open? Word doesn't provide a way to retrieve such information, but you could do it with a macro. This tip describes the approach that should be used if you decide to develop such a macro.
Making Macros Run Faster
Designing a macro to make it run faster.
Moving the Insertion Point in a Macro
One of the common things done in macros is to somehow "process" documents, which often means moving the insertion point in some manner. This tip explains how this is most easily done.
Moving the Insertion Point to the Beginning of a Line
If you need to move the insertion point within your macro, then you'll want to note the HomeKey method, described in this tip. It can be used to move the insertion point to the beginning of a line of text.
Numbers to Text, Take Three (Over a Million)
So, you need to convert a number to text that is over one million? Here is a VBA macro that will convert up to 999,999,999.
Occurrences of a Text String within a Document
You may have a need to find out how many times a certain text string occurs within a document. You can find out manually using the Find and Replace features of Word, but that won't work in a macro. The technique in this tip will work, however.
Offering Options in a Macro
When creating macros, you often need to offer a series of choices to a user. This tip demonstrates how easy it is to offer options and get feedback, using just a few commands.
Options in Creating New Files
You'd think that Word would be consistent, right? Wrong! Here's one example where Word is anything but consistent when it comes to creating a new file.
Printing Styles in a Macro
There may be times when you want your macro to print out a list of styles in the document. If so, then you can do it with a single command, as illustrated in this tip.
Printing Summary Information from a Macro
Part of the information that Word maintains about each of your documents is a summary statement, which you can define in the properties for the document. If you want to print that summary from within a macro, you can use the .PrintOut method, described in this tip.
Printing via Macro without Messages
When you are printing a document, it is not unusual to see messages (dialog boxes) periodically. When you want the printing to be done by a macro, the messages can cause unwanted interruptions. Here's a way to make the messages stop.
Problem with Add-on Macros
Add-ons can extend the capabilities of Word, sometimes significantly. These extensions are due to macros contained in the add-ons. Here are some pros and cons of enabling add-ons that contain macros you may not be aware of.
Quickly Dumping Array Contents
If you store information within a variable array, at some point in your macro you may want to get rid of that information. Here's a quick and efficient way to do it.
Removing a Directory
Your macro, in the course of doing some processing, may create a directory that you later need to delete. Here's how to get rid of it.
Removing All Text Boxes In a Document
Text boxes are a common element of many types of documents. At some point you may want to get rid of all the text boxes in a document, however. Here are several ways that you can rid yourself of all the text boxes.
Renaming a File
Need to rename a disk file from within a macro? You can do it using the Name command, described in this tip.
Repaginating in a Macro
When creating a macro that extensively processes a document, you may need to periodically force Word to repaginate the document. This can be done with the Repaginate method, as discussed in this tip.
Repaginating Your Document in a Macro
When processing a document with a macro, you may need to have the macro repaginate the text. It's easy to do using the Repaginate method described in this tip.
Resetting Character Formatting in a Macro
Want your macro to get rid of the formatting applied to a selection of text? It's easy enough to do using the Reset method, described in this tip.
Running Macros Based on Keywords
Wouldn't it be great if Word could execute a macro every time someone typed in a particular keyword or phrase? Word may not be able to do this natively, but you can add a macro that will perform the task.
Running Macros from Macros
Need to run one macro from within another macro? You can easily do it by using the Run method of the Application object, as described in this tip.
Selecting to the Next Punctuation Mark
Writing macros often involves selecting different parts of your document so that some sort of processing can be performed. If you want to select text starting at the current insertion point to the next punctuation mark, you'll appreciate the techniques discussed in this tip.
Setting a VBA Variable From a Bookmark
Bookmarks are quite helpful in a document. You may want to transfer the contents of a bookmark into a macro variable in order to process the information in the bookmark. Here are two techniques for getting at the bookmark information.
Setting the Left Indent of a Paragraph in a Macro
When using a macro to format text, you can set all sorts of attributes for paragraphs or individual characters. On attribute you can specify is how far the left margin of the paragraph should be indented. All it takes is a single command, as described in this tip.
Setting the Right Indent of a Paragraph in a Macro
Need to format your document using a macro? You can easily set the right margin for an individual paragraph by using the RightIndent property, described in this tip.
Swapping Two Strings
Part of developing macros is learning how to use and manipulate variables. This tip examines a technique you can use to exchange the contents of two string variables.
Temporarily Changing the Printer in a Macro
You can use a macro to print to any printer you have defined in Windows. It is good practice, if you are changing which printer you are printing to, to use the programming technique described in this tip to remember which printer was previously selected on the system.
Toggling Font Assignments in a Macro
If you need to quickly switch a text selection from one typeface to another, one way you can do it is with a macro. This tip presents such a macro, along with an alternative method of quickly changing typefaces.
Understanding the While...Wend Structure
One of the basic programming structures used in VBA is the While ... Wend structure. This structure helps to make the controlled repeating of programming statements quite easy.
Using AutoCorrect to Start Macros
As you are typing, AutoCorrect provides a "check" that what you are entering doesn't match some pre-defined error triggers. The idea is to make sure your text reflects what you intended to write, rather than what you really wrote. This tip discusses the concept of whether AutoCorrect can be used to not only "fix" what you type, but also start macros that could do even more processing.
Using Mandatory Form Fields
When using form fields to gather information from users of your documents, you may want to make sure that some of the fields aren't skipped over. Here's a way you can make sure that information is entered into a particular form field.
Word's Object Model
Understanding Word's Object Model and how it relates to macros in VBA.