Getting to know the object model
Much of the trick to writing VBA macros for Word is knowing Word's object model - that is, the objects available in a Word macro and the properties and methods available for each type of object.
One way to get to know the object model is to let the VBA editor tell you about it. When you type a period after an object name, the VBA editor pops up a helpful list of the properties and methods available for that object. Then you can scroll down to the property or method you want to use and press Tab to insert it into your macro. This way is one of the best to discover which properties and methods are available for any given object.
Another way to find out which objects, properties, and methods are available - perhaps the best way - is to record a macro that comes close to accomplishing what you want to do and then switch to the VBA editor to see the code that was generated for the macro. Suppose that you want to write a macro that marks index entries. If you record a macro and then press Alt+Shift+X to mark an index entry, Word generates VBA code similar to this:
ActiveDocument.Indexes.MarkEntry Range:=Selection.Range, Entry:= _ "Object model", EntryAutoText:="Object model", CrossReference:="", CrossReferenceAutoText:="", BookmarkName:="", Bold:=False, Italic:=False
In this example, you can tell that you use the MarkEntry method of ActiveDocument.Indexes to mark an index entry, and that this method takes several arguments: Range, Entry, EntryAutoText, CrossReference, CrossReferenceAutoText, BookmarkName, Bold, and Italic.
A method is an action that an object can perform. When you call a method, the object performs the action associated with the method. For example, the following statement copies the contents of the current selection to the Clipboard:
Some methods require arguments, which supply data for the method to work with. For example, the Selection object's InsertAfter method accepts a text string as an argument. The text string contains the text that's inserted after the selection.
To use arguments, simply list the argument values you want to pass to the method, like this:
Selection.InsertAfter "Some text"
In this line, Selection is an object, InsertAfter is a method, and "Some text" is an argument.
If a method requires two or more arguments, separate them with commas, like this:
Selection.StartOf wdParagraph, wdExtend
In this example, the arguments are positional because Word interprets the meaning of each argument based on the order in which you list them. In Word, you can also pass arguments by using argument names, as in this example:
Selection.StartOf Unit:=wdParagraph, Extend:=wdExtend
When you use methods that require arguments, the VBA editor displays tips that let you know which arguments the method expects.
In VBA, you can also enclose the arguments in parentheses, like this:
However, the parentheses aren't required. Note that it's okay to use both styles - with and without parentheses - within a macro.
In this tutorial:
- Programming with VBA
- Basic Structure of VBA Macros
- Understanding the Basic Elements of VBA
- Working with Variables and Data
- Using static variables
- Using Strings
- Of Objects, Properties, and Methods
- Getting to know the object model
- Using the With statement
- Controlling Your Programs
- The ElseIf structure
- For/Next Loops
- User Input and Output
- User-Defined Procedures and Functions