Microsoft KB Archive/154567

= ACC95: Create and Date Stamp a Word Document Using OLE Automation =

Article ID: 154567

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Word 95 Standard Edition

-



This article was previously published under Q154567



Moderate: Requires basic macro, coding, and interoperability skills.



SUMMARY
This article demonstrates how to use OLE Automation to create a Microsoft Word document and add a date stamp.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications with Microsoft Access for Windows 95" manual.



Example - Create and Date Stamp a Microsoft Word document
The following example creates a new Microsoft Word document, inserts some text and formats it, and then inserts a text date stamp.

To run this code:

 Open Microsoft Word. When the code is run from Microsoft Access, Microsoft Word will remain open. Open a Microsoft Access for Windows 95 database and create a new module. Add a Reference to the Word95Acc.TLB reference file. To do this, on the Tools menu, click References. Click the Browse button and locate the Wrd95Acc.dll file. Usually, this file is located in the \ACCESS folder. Select the file and click OK. The Word 95 reference library is now referenced by the active Microsoft Access database.  Type the following function in the Module window: Function CreateDoc Dim ThisWord As Word95ACC.Word95Access Set ThisWord = CreateObject("Word.Basic") With ThisWord .AppMaximize ' Create a new document based on the default template (usually         ' Normal) .FileNewDefault .FormatFont _ Points:=22, _ Bold:=True, _ Italic:=True .INSERT "Welcome to Word OLE Automation" .InsertPara .FormatFont _ Points:=10, _ Bold:=False, _ Italic:=False .INSERT "Report Created:" ' Insert the date as text, not as an updateable field .InsertDateTime _ DateTimePic:="YYYY MM DD HH:MM:SS", _ InsertAsField:=False .InsertPara End With End Function 

To run the function, open the Debug window, type the following line, and then press ENTER:

?CreateDoc

NOTE: In both examples, when the OLE Automation object goes out of scope, the instance of Microsoft Word is unloaded unless the object was created from a previous instance (already opened). It is possible to set the object to a global variable so that it does not lose scope until the application is closed.

