Part III: More-Advanced Access Techniques
808
Using Office’s macro recorder
Using Automation is not a difficult process when you understand the fundamentals. Often, the
toughest part of using Automation is knowing the proper objects, properties, and methods to use.
Although the online help for Automation servers such as Word and Excel can be useful, the easiest
way to quickly create Automation code for Office applications like Word is to use Word’s macro
recorder.
Most Office applications have a macro recorder located on Word’s Developer ribbon tab (see
Figure 22.8). When activated, the macro recorder records all user actions, such as menu selections
and button clicks, and creates VBA code from them.
FIGURE 22.8
The Word macro recorder is a powerful tool to help you create Automation code.
Open VBA Editor
View all macros Record macro
Developer tab
You likely noticed that the code example in the “Inserting pictures by using bookmarks” section uses
some funny-looking arguments. For example:
WordObj.Selection.Goto What:=wdGoToBookmark, Name:=“Picture”
This statement includes two named arguments (What and Name), a somewhat advanced VBA program-
ming concept.
The named arguments in this statement are included to make it easy to understand the purpose of the
statement. A named argument is comprised of the argument name (What or Name), followed by a colon
and equal sign (:=), and then the value assigned to the argument.
Named arguments are most useful in cases where a procedure includes a lot of optional arguments and
you aren’t using all the optional arguments. When using named arguments, you don’t have to use
“placeholder” commas in the procedure call’s argument list. For instance, the Selection object’s
GoTo method has four optional arguments, but the code example uses only two of those arguments.
Using named arguments simplifies writing code that uses the GoTo method.
A brief word about named arguments