Access VBA Macro Programming

(Joao Candeias) #1

oWdoc.SaveAs ("MyTest")
oWdoc.Close
oWd.Quit
Set oWdoc = Nothing
Set oWd = Nothing
End Sub


The first line declares the variables for the application and the document objects. Because
you have a reference to the Object Library included, you will see the Word objects, methods,
and properties appearing in the drop-down lists as you type the code in. If you did not include
the reference to the Object Library, VBA will not recognize these object types and the
application will fail.
You then use the application variable (oWd) to hold the created object for the Word
application. Note that in theCreateObjectparameter the description goes inside quote marks.
This description uses the class name for the object and does not appear automatically because it is
being entered as a string. You are not offered a list of choices for it. The string "Word.Application"
is the class name for the application object within Word, which is why it is used here.
TheoWdocvariable is now set to hold a new document based on the application variable
(oWd). This is exactly the same as if you had loaded Word and then clicked the Office Start
button and created a new Word document.
The text “My new Word Document” is then added to the first section of the document.
This appears on the first section because you loaded the Word document, basically in the
same way as if you had opened Word and then opened the file in Word. The cursor
automatically defaults to the top of the document when this happens, and exactly the same
thing happens in your code, because you are emulating the document being opened.
The document is saved as MyTest to the Documents folder on the local hard drive, and the
Word document is then closed. Leaving it open causes reservation problems, which means
that your application has exclusive write access to it. If anyone loads this document into
Word, they can only load it as read-only because your application has the reservation on it.
This can also cause problems when exiting Access. Because this is a virtual application, the
document stays in memory if it is not closed (even if Access is shut down) properly, even
though Access only had a reference to it. However, this particular instance of Word does not
appear on the Windows taskbar because it was created virtually in code. The only way to
detect its presence is to look in Task Manager.
If you do not close the virtual object down properly, when Windows shuts down, the
virtual Word application will ask if the document needs saving because it still thinks there is
an open document. This can be extremely confusing to the user, since they may have no idea
that a Word application was open. To avoid this situation, the variablesoWdandoWdocare
set to Nothing, and all memory held by them is released.
Try loading your newly created file into Word and you will see that it works as a perfectly
normal Word document, just as if you created it using Microsoft Word.
This is a very simple example of manipulating Word from within Access using VBA. This
can be very useful—for example, you could have a standard document with tables that the
macro populated from the database data. You can run your macro and the data will be
transferred into the document tables.


Chapter 17: Using Access to Interact with Other Office Programs 223

Free download pdf