Microsoft KB Archive/142475

= ACC95: How to Use OLE Automation to Create a Word Document =

Article ID: 142475

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 95 Standard Edition

-



This article was previously published under Q142475



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



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

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.



How to Create a New Microsoft Word Document
 Create a new folder called Examples on drive C. Open a Microsoft Access 7.0 database and create a new module.  Type the following function in the Module window: Public Function wordtest Dim w As Object Set w = CreateObject("word.basic") With w           .filenew template:="Normal" .INSERT "This is a test." .editselectall .allcaps 1 .bold 1 .formatfont points:=14, underline:=1, font:="arial" .filesaveas Name:="c:\examples\wordtest.doc" End With End Function  Press CTRL+G to open the Debug window and type the following word:

wordtest

 Start Microsoft Word 7.0, and view the new file c:\examples\wordtest.doc.

How to Create a New Microsoft Word Document Using a Bound Form Control
 Create a new table and name it Test Table.</li> Add a MyOleField field as an Ole Object data type.</li> Create a new form based on the Test Table table by using the AutoForm: Columnar Wizard.</li>  Add a command button to the form with the following properties: Name: Command4 Caption: My Button OnClick: [Event Procedure] </li>  Enter the following code for the command button's OnClick property event procedure: Private Sub Command4_Click

Dim worddoc As Object With MyOleField .Class = "Word.Document" .OLETypeAllowed = acOLEEmbedded .Action = acOLECreateEmbed .Verb = acOLEVerbInPlaceUIActivate .Action = acOLEActivate End With

Set worddoc = Me![myolefield].Object.Application.wordbasic

With worddoc .INSERT "This is a test." .editselectall .allcaps 1 .bold 1 .formatfont points:=14, underline:=1, font:="arial" End With

Command4.SetFocus

End Sub </li> Switch the form to Form view and click the My Button button. Note that the data formatted by Microsoft Word is inserted into the OLE Field.</li></ol>

<div class="references_section">