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

This article was previously published under Q142475
This article has been archived. It is offered "as is" and will no longer be updated.
Moderate: Requires basic macro, coding, and interoperability skills.
This article demonstrates how to use OLE Automation to create andmanipulate a Microsoft Word document.

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

How to Create a New Microsoft Word Document

  1. Create a new folder called Examples on drive C.
  2. Open a Microsoft Access 7.0 database and create a new module.
  3. 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
  4. Press CTRL+G to open the Debug window and type the following word:
  5. 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

  1. Create a new table and name it Test Table.
  2. Add a MyOleField field as an Ole Object data type.
  3. Create a new form based on the Test Table table by using the AutoForm: Columnar Wizard.
  4. Add a command button to the form with the following properties:
          Name: Command4      Caption: My Button      OnClick: [Event Procedure]
  5. 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						
  6. 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.
For more information about creating objects with OLE Automation, search onthe phrase "Create a Word Document using OLE Automation," and then"CreateObject function" using the Microsoft Access for Windows 95 AnswerWizard.

Microsoft Access, "Building Applications with Microsoft Access for Windows95, version 7.0, Chapter 11, "Communicating with Other Applications," pages262-290.

Article ID: 142475 - Last Review: 12/29/2014 10:50:56 - Revision: 2.2

Microsoft Access 95 Standard Edition

  • kbnosurvey kbarchive kbhowto kbprogramming KB142475