Part III: More-Advanced Access Techniques
810
After you finish recording a macro, you can view the VBA code created by the macro recorder. To
view the macro code, click the Macros button on the ribbon to display a list of all saved macros
and select the macro that you recorded. Click the Edit button to display the VBA editor with the
macro’s code. Figure 22.11 shows the VBA editor with a macro that recorded the creation of a new
document using the Normal template.The macro includes inserting a picture using the Picture
from File command in the Insert ribbon’s Illustrations group.
FIGURE 22.11
The VBA code recorded by Word’s macro recorder
Macro description Macro name
Most recorded Word macros make copious use of the Application object. The Application object is
topmost in the object hierarchy exposed by Word. Even though the Application object may not be
explicitly included in the VBA statements created by the macro recorder, it’s the root of virtually all
objects referenced by the macro. For example, the preceding macro uses the following code to cre-
ate a new document:
Documents.Add Template:=“Normal”, _
NewTemplate:= False, DocumentType:=0
Although this statement doesn’t explicitly include a reference to the Application object, the
Documents collection is owned by Application. To use this code for Automation, copy the code
from the Word VBA editor, paste it into your Access procedure, and create an explicit Application
object:
Dim WordObj as Word.Application
Set WordObj = New Word.Application
WordObj.Documents.Add Template:=“Normal”, _
NewTemplate:= False, DocumentType:=0
The macro recorder enables you to effortlessly create long and complex Automation code without
needing to read the Word or Excel documentation. Try using the macro recorder to generate VBA
code instead of typing it from scratch.