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.
Accessing Paragraphs in a Macro
Need to process a document, paragraph by paragraph, in a macro? It's easy to do once you understand that Word's object model allows you to access each paragraph individually.
Accessing the Dirty Flag
When creating macros, you might want to know if a user has made changes in the document or not. Here's how to figure that out.
Adding a Macro to the Quick Access Toolbar
One of the easiest ways to quickly access a macro is to assign it to the Quick Access toolbar. Here's how you can make the assignment.
Adding Automatic Time Stamps
Your computer knows the current date and time, and Word provides ways you can get that date and time into your document. Doing so easily and in the format you want may be a challenge, but it is not an insurmountable one.
Need to add parentheses around some word or phrase? Here's a quick macro that makes this simple edit in one step.
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.
Aligning Paragraphs in a Macro
Using a macro to format your document (or portions of your document) is not all that uncommon. If you want your macro to change paragraph alignment within the document, there are just a couple of properties you need to focus upon.
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.
Assigning a Macro to a Shortcut Key
Do you have a macro that you use frequently? Using the file menu to access the macro can be time consuming. This tip talks about how to create a shortcut key that will cut out the menu.
Attaching Macros to Documents
When you distribute documents to other people, you may want those documents to have associated macros that the reader can access. This can be a problem, as discussed 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.
Automating a Manual Process with a Macro
The entire purpose of macros is to allow you to automate repetitive or tedious tasks with relative ease. How easy the automation is, though, depends on the data you are working with in the first place. Here's one example.
Bumping Numbers in a Document
If your documents include words that contain numbers (such as a list of parts numbers) you may need a way to increment those numbers. Here's a way you can do it quickly using a macro.
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.
Word makes it easy to insert today's date in a document, but not as easy to insert a date X number of days in the future. This tip discusses some ways that you can get around this apparent limitation.
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.
Can't Edit Macros
Load up documents created on older versions of Word, and you may find that you can't edit the macros you are used to using. This tip discusses what that occurs and offers a few ideas on how to solve the situation.
Changing a Macro Description
Part of documenting macros is to provide a good, succinct description of what they do. Changing the description of an existing macro is easy if you apply the techniques described in this tip.
Changing Built-in Word Commands
Want to replace Word's internal commands with your own macros? It's easy to do if you know the key discussed in this tip.
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 Pronoun Gender
Sometimes it is necessary to change the gender of various pronouns used in a document. If you have a stock document that could be directed to either a male or female, you might want to make universal changes in the pronouns to reflect the gender of the document's intended recipient. Here's one approach you can use.
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 the Default Drive
Macros can be used to read and write all sorts of files. If those files are on a different drive than the current one, you'll appreciate that you can change the default drive very easily using the ChDrive command.
Changing the Format of Existing Dates
There are a myriad of ways in which a date can be formatted—day first, month first, number of digits in the year, etc. If you want to change the way a date is formatted once it is in your document, how you do so depends on the way in which the date was inserted to begin with.
Changing ToolTips for a Macro Button
Want to change the ToolTip that appears when you hover the mouse pointer over a tool on the Quick Access Toolbar? Here's how to make it say just what you want said.
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.)
Character Frequency Count
Word collects a wide range of statistics about your documents, but one of the things it doesn't collect is how many times individual characters appear in the document. (This would be considered a "frequency count" of each character.) You can create a macro to count the characters, but the approach you use to the macro will determine how efficiently the requisite counts are collected.
Checking for a Text Selection Length
Need to know if the user selected some text before running your macro? Here's how to make that check.
Clean Up Your Macro List
Macros are part of the document and template that are loaded into memory when you open a file. If you have "junk" macros in the template or file the document takes longer to load. Periodically reviewing your macros and deleting unneeded ones is a good practice.
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.
Clearing the Undo Stack in a Macro
When writing a macro, you may need a way to clear the undo stack. This can be done with a single command, as described in this tip.
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 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.
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.
Controlling the Italic Text Attribute
If you are formatting your document by using a macro, you may need to make some of your text italics. You do that by changing the Italic property for the selection.
Converting Inches to Points
Typographical measurements are often expressed in points. There are several formatting settings that, when accessed through a macro, require measurements to be made in points. Fortunately, VBA provides a function that can handle the conversion from inches into points for you.
Converting Numbers to Strings
VBA is great at working with both strings and numbers. At some point you may have a number you need to convert to a string. This is easy enough to do using the Str function, described in this tip.
Converting Numbers to Text
Got some numbers you need spelled out? Here's a handy macro that can convert numbers like "123" to words like "one hundred twenty-three."
Converting Paragraphs to Comments
Want to pull text from a bunch of paragraphs and stuff that text into comments? It's easy to do using the macro presented 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 Comments
One of the strong suits of macros is that they can process the information in a document quickly and reliably. For instance, if you have text that uses a reliable pattern, you can find that text and convert it to some other use in a document, such as comments. This tip shows you how.
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.
Copying a File in VBA
Need to have your macro copy a file from one place to another? It's easy to do using the FileCopy command, described in this tip.
Copying a Range of Pages in a Macro
Do you need to copy, within a macro, a range of pages? Because pages can be so fluid in Word, this can be a bit tricky. There are ways to derive the desired information, though, as described in this tip.
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.
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.
Counting Words the Old Fashioned Way
One way to specify word count is to count characters and divide by five. If you still need this old-fashioned way of counting, here's how you can let Word do the heavy calculating for you.
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 New Document in VBA
When working with documents in a macro, it makes sense that you may need to create a document from time to time. Here's how easy it is.
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.
Declaring variables in a macro is good programming practice. Here's how to do it and how to make sure they are all declared.
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.
Deleting a Macro
ScreenTips can appear with or without shortcut keys displayed in them. Here's how to control whether they appear or not.
Deriving an Absolute Value
Want to know the absolute value of a number? It's easy to derive in VBA by using the Abs function.
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.
Detecting if the Insertion Point is Inside a Bookmark
When processing a document using a macro, you may need to know if the insertion point is within a bookmark or not. This is easy to determine by simply looking at the number of members in the Bookmarks collection, as described in this tip.
Detecting the Beginning of a Sentence in a Macro
Macros can make life easier, as they provide a fast and efficient way of processing text in a document. Such is the case if you need to modify lots of text to be consistent. This tip examines ways to make sure the capitalization of text at the beginning of a sentence is correct.
Determining a Paragraph's Style in VBA
When processing a document via a macro, it is often helpful to understand what style has been applied to a paragraph. You can figure this out by using the Style property, described in this tip.
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 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 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 Differences Between Dates
Do you need to do some simple math using dates in a your macro? One of the easy functions you can use is the DateDiff function.
Determining How Many Styles are Available
Got a macro that processes or uses styles? You definitely need to know how many styles Word has available in the document. Use the Count property of the Styles collection to determine the figure.
Determining How Many Windows are Open
You can open multiple documents at the same time in Word, and each document occupies its own document window. Here's a way you can determine, in a macro, how many of those windows are open at the current time.
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 if a Text Selection Exists
Macros are often designed to be run on just a selected portion of a document. It is a good idea to make sure that the user actually made a selection before running your code. Here's how to determine if that necessary selection exists.
Determining if Caps Lock is On
If your macro needs to determine the status of the Caps Lock key, you need the code in this tip. Just use the Information property to query the key's state.
Determining if Num Lock is On
Need 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.
Determining if Overtype Mode is Active
Your macro may need to determine if the user has overtype mode turned on. You can find out the overtype status easily by using the Overtype property.
Determining the Current Directory
When creating macros, it is often necessary to know which directory is the default. Here's how you can find out by using the CurDir command.
Determining the Current Page Number
While your macro is processing the text in your document, you may need a way to determine the current page number where the insertion pointer is located. This is done by using the Information method, as described in this tip.
Determining the Day of the Month
When writing macros, you may need to know which day of the month a particular date represents. Here's how to use the Day function to determine that information.
Determining the Day of the Year
Need to find out the day of the year for a particular date? It's easy to do if you are using a macro. All you need to do is use the DatePart function.
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 Hour of the Day
Need to figure out the hour represented by a particular time value? It's easy to do in a macro; just use the Hour function, described in this tip.
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.
Determining the Month of the Year
Your macro code may need to determine the month of the year represented by a particular date. You can find the desired information by using the Month function.
Determining the Number of Fonts Available
When creating a macro, you may need to figure out how many fonts are available to Word. You can do this using the FontNames collection.
Determining the Number of Pages in Your Document
If your macro needs to know how many pages are in your document, you can use the Information method to get the desired info. It just takes a single line added to your macro.
Determining the Number of Paragraphs in a Document
When using a macro to process a document in some way, you often need to know the number of paragraphs in the document. (This is especially true if you are processing the document paragraph by paragraph.) You can figure out how many paragraphs there are by examining the Paragraphs collection.
Determining the Size of a File
When processing a document using a macro, you may need to know the precise size of a particular file. The way you figure this out is to use the FileLen function, described 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.
Determining the Week of the Year
If you are working with dates in a macro, you may need to determine which week of the year a date falls within. This can be determined with the DatePart function.
Displaying a Message in the Status Bar
A great place for your macro to display status information is, well, in the status bar. Displaying the information is easy, as this tip points out.
Displaying Properties Dialog Box in a Macro
Word keeps track of identifying and statistical information about a document and makes that information available in the Properties dialog box. If you want to display that information within a macro, then you may be at a loss as to how to do it.
Displaying the Developer Tab
The Developer tab of the ribbon is the gateway to many advanced features in Word, including those features related to macros. Problem is, the Developer tab is not visible by default in Word. Here's how to make sure it remains displayed on your system.
Displaying the Navigation Pane when Opening a Document
The Navigation pane can be a big help in moving around a document. If you want to make sure it is always displayed for your documents, you'll appreciate the ideas in this tip.
Displaying the User Name in the Status Bar or Title Bar
Sometimes it can come in handy to know who the current computer user is, as far as Word is concerned. This tip presents ways you can display the user's name in either the status bar or the title bar.
Dissecting a String
Want to pull a string apart in a macro? It's easy using the string functions introduced 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.
Finding a Change in Typeface
When you format a document, you can go so far as to change the font of each character in the document. This may be overkill, but it is not unusual to have a character here or there that uses a different typeface than the previous characters. This tip provides a macro that that allows you to step through the characters in a document and locate places where the typeface changes.
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.
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.
Don't want a particular macro to be visible in the Macros dialog box? Once you understand the criteria that Word uses when determining which macros to display, you can use this understanding to your benefit. This tip examines three ways you can keep your macros out of the dialog box.
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 Break with a Macro
Inserting a break in your document is easy. You may think that inserting one using a macro is more complex, but it isn't. Just use the InsertBreak method, described 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.
Jumping to the Start or End of a Document
When creating macros, it is often necessary to move the insertion point around the document so that text can be processed in the manner desired. One of the most common movements is to move the insertion point to the beginning or ending of the document. Here's how to do it.
Jumping to the Top of a Page
Do you want to easily jump to the top of a page in your document? You can use the Go To command to make the shift, or you can use the short macro described 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.
Making Macros Available without Using the Normal Template
If you save your macros in your Normal template, they'll be available whenever you are using Word. What if you don't want to put them in the Normal template, though, and you still want them available? Here is a great way to make that happen on your system.
Minimizing the Ribbon for a Document
Want the ribbon to be minimized for a particular document? Word may not allow you to get the exact result you want, as this tip explains.
Modifying Behavior of the Open Dialog Box
The Open dialog box is one that few of us think about, but you can control how it behaves with a little bit of macro coding. This tip provides a few examples you can use right away.
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.
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.
Moving Through a Table in a Macro
Do you need to step through a table, cell by cell, in a macro? It's easy to do using the Move method, as described in this tip.
Moving to the Start or End of the Real Document
The main body of your text is only one part of what makes up the entire document. Documents can consist of other elements, as well, such as headers, footers, text boxes, footnotes, etc. How can you create a macro that will move the insertion point to the beginning or end of the main body of your text, regardless of where the insertion point is currently located? It's easier than you think.
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.
Passing a Data File Name via Command Line to a Macro
Using the command line to pass paramaters to a program is a common occurrence. Using the command line to pass parameters to a macro running within the program is an entirely different kettle of fish.
Passing Parameters to Functions
Functions can be used to perform repetitive tasks and return values to your main program. You can also pass values to a function, however, which makes them even more useful.
Printing a Macro List
Need a list of all the macros you've created? Word doesn't provide a way to create such a list, but you can use the workarounds and ideas presented in this tip to get what you need.
Printing Graphic Thumbnails
If you are doing work with a lot of graphics, it may be helpful to create a summary page that contains thumbnail representations of all the graphics. Here's a handy macro that can make quick work of such a page.
Printing Shortcut Key Assignments from a Macro
Need to know what shortcut keys are defined? You can use a single macro command line to print out the definitions.
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.
Putting a Macro Name on the Quick Access Toolbar
In older versions of Word you could customize the program so that either icons or text (or both) could appear on the toolbars. No so in Word anymore. This tip takes a look at what you can do and provides a few pointers.
Quickly Clearing Array Contents
An array of variables is a powerful element of VBA programming. If you want to clear what is stored in an array, here's a quick 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 a Macro from a Shortcut Key
Associate a macro with a shortcut key, and at some time you may want to break that association. (Perhaps so the shortcut key can be used for a different purpose.) Here's how to make sure that a macro is not associated with a given shortcut key.
Removing Shortcuts for Macros
Word apparently has a bug in it that makes it very difficult to remove a shortcut key previously associated with a macro. This tip examines the problem and looks at a possible solution.
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.
Renaming a Macro
Want to give your macros a different name than they currently use? It's easy to do using the VBA Editor as described here.
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.
Need to repeat an action a whole bunch of times? You can do it a time or two using keyboard shortcuts, but you'll need a macro (like the handy one in this tip) to make repeating a lot of times possible.
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.
Resetting Character Formatting in a Macro
Shortcut keys are a great way to apply styles to text in a document. You can easily create a shortcut key assignment for any style you desire. This tip explains how.
Reversing a String
Need to reverse all the characters in a string? You can do so by using the function described in this tip.
Reversing All the Paragraphs in a Document
Documents consist of a series of paragraphs, arranged in the order in which you need them. What if you need to reverse the order of all the paragraphs? The ideas in this tip will help you complete the task quickly and easily.
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.
Saving a Document in a Macro
If you develop a macro to process your document, you may want the macro to save the document to disk. This is easily done using the Save or SaveAs commands in your macro code.
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 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.
Scaling Graphics in a Macro
If you need to make sure that the graphics in a document are all scaled similarly, you'll love the macros presented in this tip. Use them to scale individual graphics or all the graphics.
Selecting a Bookmark in a Macro
Bookmarks can be very handy in a document. Word provides a VBA command you can use to easily select any of those bookmarks.
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.
Specifying a Paper Tray in a Macro
You may want to use a macro to process and then print your document. Part of that printing may involve specifying which of your printer's paper trays Word uses. Here are some considerations you need to take in account if you want to select a paper tray in your macro.
Swapping Two Numbers
When developing a macro, you may need to swap the values in two variables. It's simple to do using the technique 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.
Trimming Spaces from Strings
When processing text with a macro, you often need to remove extraneous spaces from the text. VBA provides three handy functions to remove those leading or trailing spaces easily.
Turning Off Screen Updating
When working with macros, you can often speed up processing by turning off the updating of the screen. Best news is that it takes only a line or two added to your macro.
Understanding Document Variables
When working with macros, you may want to create a variable that will remain constant from one instance of the macro to another. This is a great use for document variables, as described in this tip.
Do some macro programming in VBA and you'll quickly find out that you can use functions to extend the power and flexibility of your programs. Here's why functions are so powerful and how you can put that power to work.
Formulas created in a macro have a specific order in which operations are performed. This is known as precedence, as explained in this tip.
The heart of creating powerful programs in VBA is to understand how to create subroutines. These structures allow you to break your large programming tasks down into bite-sized chunks that can be used again and again.
Understanding the For ... Next Structure
Spend any time creating Word macros, and sooner or later you will need to repeat some of your programming code a certain number of times. That's where the For ... Next structure comes into play.
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.
Understanding the Select Case Structure
Programming structures are an important tool used by any programmer. The VBA language used by Word's macros includes several powerful programming structures, including the Select Case structure, described in this tip.
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.
Variables are used quite frequently in programming, and VBA programming is no exception. Each variable can have a data type that defines the characteristics of the data that can be stored in the variable. This tip examines the purpose of variables and the different data types you can use to define them.
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 Call to Run VBA Macros
An elegant way to run macros from within macros is to use the Call statement. In order to use it, you need to provide a reference from the calling project to the one being called.
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.