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

ID: Q142475

The information in this article applies to:

SUMMARY

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

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.

MORE INFORMATION

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:

       wordtest

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.

REFERENCES

For more information about creating objects with OLE Automation, search on the phrase "Create a Word Document using OLE Automation," and then "CreateObject function" using the Microsoft Access for Windows 95 Answer Wizard.

Microsoft Access, "Building Applications with Microsoft Access for Windows 95, version 7.0, Chapter 11, "Communicating with Other Applications," pages 262-290.

Keywords          : kbole IntpOlea 
Version           : 7.0
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: August 29, 1997