Access VBA Macro Programming

(Joao Candeias) #1
You can add a reference by selecting Tools | References from the menus. All available
reference files will appear in a dialog. You need to select the Microsoft Word Object Library
and check the check box next to it, as shown in Figure 17-1.
Note that the location shown at the bottom of the References window points to an OLB
file that is basically the Object Library for Word. The location points to the OLB file in the
directory where Microsoft Office was originally installed. Click OK and you will be ready to
use Word in VBA.
Here is a sample of code to create a new Word document and to save it to the local hard
drive. This code will produce the same results whether Word is loaded or not, but it always
looks more spectacular if Word is not running onscreen—making it seem as if you have done
something very clever!

Sub Test_Word()
Dim oWd As Word.Application, oWdoc As Word.Document
Set oWd = CreateObject("Word.Application")
Set oWdoc = oWd.Documents.Add
oWdoc.Sections( 1 ).Range.Text = "My new Word Document"

222 Microsoft Access 2010 VBA Macro Programming


Figure 17-1 Selecting the Microsoft Word Object Library
Free download pdf