Tips, Tricks, and Answers
The following articles are available for the 'Creating Macros' topic. Click the article''s title (shown in bold) to see the associated article.
Can't Save a Macro
Macros can make your use of Word faster and easier than ever before. What do you do, however, when you try to save a macro and Word won't let you do it? There could be several explanations for why this is happening.
When writing a macro, a common task is the need to compare two strings. You can do this by "normalizing" the strings, as described in this tip.
Continuing Macro Lines
Program a macro, and you can easily find that some lines get very long. If you want to shorten the lines so they are more understandable, here's the key to breaking them up.
Controlling the Hidden Text Attribute
Want your macro to change the Hidden attribute for some text in your document? It's easier to change than you might think.
Counting Open Document Windows
When creating macros, it is sometimes necessary to know how many documents are open in Word. This is relatively easy to do using the Windows.Count property, but it can be more troublesome to figure out what is in each of those windows.
Debugging a Macro
Create a macro and you are faced with the (sometimes) challenge of debugging it. Here's how to make that task as simple as possible.
Determining an ANSI Value
You may need to determine the numeric value of a character in a macro. You can do that using the Asc function, described in this tip.
Determining If a Number is Odd or Even
Need to know if a number in a macro is odd or even? It's easy to figure out with the simple calculation shown in this tip.
Determining the Upper Bounds of an Array
When working with variables in a macro, you may need to know the upper boundary dimension for an array. This can be discovered by using the UBound function, described in this tip.
Dissecting a String
Want to pull a string apart in a macro? It's easy using the string functions introduced in this tip.
Locating the My Documents Folder
Need to do some macro processing of documents in the user's My Documents folder? First step is to figure out where the folder is located. Here's the easiest way to figure out the path.
Moving the Insertion Point to the End of a Line
When writing a macro to process the text in a document, you may need to move the insertion point to the end of a line. This is done with the EndKey method, described in this tip.
Repeating In a Macro
Macros are often used to process information stored in documents. Usually the processing involves some sort of iterative action within the document, such as doing a search and every time the searched-for item is found, perform a task. Understanding how to perform this type of repeating can be very powerful.
Saving Changes when Closing
If you write a macro that makes changes to a document, you may want that macro to save those changes. There are several ways you can implement the proper code to do this, as described in this tip.
Saving in a Macro Using a Desired File Name
Need to save a new document, from within a macro, to a specific file name? If you use the Record Macro capabilities of Word, you won't get the results you want. The information in this tip provides the answers, as well as a whole lot of different ways you can approach the problem.
Saving in Document Format from a Macro
Saving a document in a different format is easy if you are manually using the Save As command. Saving a document in an existing macro, in the format you want, is a bit trickier unless you know how to use the SaveAs method, described in this tip.
Turning on Large Icons in a Macro
Word includes the ability to display toolbar icons in two sizes: regular and large. You can turn on the large icons by using the Customize dialog box, or you can turn them on in a macro, as described in this tip.
Formulas created in a macro have a specific order in which operations are performed. This is known as precedence, as explained in this tip.
Understanding the If ... End If Structure
One of the powerful programming structures provided in VBA allows you to conditionally execute commands. The If ... End If structure is easy to implement once you grasp what it does.
Using Message Boxes
If your macro needs to communicate with a user, one simple way to do it is to use a message box. Here's how to use this feature of VBA.
Writing a Macro from Scratch
Creating macros can help extend what you can do in Word. If you work with macros, you know that creating macros from scratch results in the most flexibility. Here's how to create your own macros from scratch.