ACC: Sending the Current Record to Word with OLE Automation

Last reviewed: August 29, 1997
Article ID: Q124862
The information in this article applies to:
  • Microsoft Access versions 2.0, 7.0

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article describes how you can merge the current record in a Microsoft Access object into a document in Microsoft Word version 7.0 or earlier, and open it in print preview.

For information about how to merge the current record with a Microsoft Word 97 document, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q131583
   TITLE     : ACC: Sending the Current Record to Word 97 with
               Automation

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.

NOTE: Visual Basic for Applications (used in Microsoft Access for Windows 95 version 7.0) is called Access Basic in version 2.0. For more information about Access Basic, please refer to the "Building Applications" manual.

MORE INFORMATION

The following steps demonstrate how to create a Microsoft Word document and a Microsoft Access form, which are then used to merge the current record from Microsoft Access into Microsoft Word:

  1. Start Microsoft Word and create the following new document:

          First Last
          Address
          City, Region, PostalCode
    

          Dear Greeting,
    

          Northwind Traders would like to thank you for
          your employment during the past year. Below
          you will find your photo. If this is not your
          most current picture, please let us know.
    

          Photo
    

          Sincerely,
    

          Northwind Traders
    

  2. Create a bookmark for the word "First." To do so, select the word "First," click Bookmark on the Edit menu, type "First" (without the quotation marks) in the Bookmark Name box, and then click the Add button.

  3. Repeat step 2 for the Last, Address, City, Region, PostalCode, Greeting, and Photo fields.

  4. Save the document as Olemerge.doc in the root folder (directory) of drive C.

  5. Start Microsoft Access and open the sample database Northwind.mdb (or NWIND.MDB in version 2.0).

  6. Open the Employees form in Design view.

  7. In the General section of the form module, declare the following global variable:

          Dim Word As Object
    

  8. Add a command button to the form and set the button's OnClick property to the following event procedure.

    In version 7.0:

           Sub Command40_Click()
           On Error GoTo CatchBlanks
           Dim Word As Object ' Declare in the module to view the print
                              ' preview after the Sub ends
    
              DoCmd.GoToControl "Photo"
              DoCmd.DoMenuItem acFormBar, acEditMenu, acCopy, , acMenuVer70
              Set Word = CreateObject("Word.Basic")
              Word.FileOpen ("C:\OLEMERGE.DOC")
              Word.EditGoto "Last"
              Word.INSERT CStr(Forms![Employees]![LastName])
              Word.EditGoto "First"
              Word.INSERT CStr(Forms![Employees]![FirstName])
              Word.EditGoto "Address"
              Word.INSERT CStr(Forms![Employees]![Address])
              Word.EditGoto "City"
              Word.INSERT CStr(Forms![Employees]![City])
              Word.EditGoto "Region"
              Word.INSERT CStr(Forms![Employees]![Region])
              Word.EditGoto "PostalCode"
              Word.INSERT CStr(Forms![Employees]![PostalCode])
              Word.EditGoto "Greeting"
              Word.INSERT CStr(Forms![Employees]![FirstName])
              Word.EditGoto "Photo"
              Word.EditPaste
              'Word.FilePrint 0
              ' To send the record directly to the printer, unremark the line
              ' above, and remark the next two lines below.
              Word.appmaximize "",1
              Word.FilePrintPreview
              Word.AppActivate "Microsoft Word"
              Exit Sub
    
           CatchBlanks:
               If MsgBox("Error sending one field, it may be blank. Would _
                  you like to continue?", 52) = 6 Then
                  Resume Next
               Else
                  Exit Sub
               End If
    
          End Sub
    
       In version 2.0:
    
       NOTE: In the following sample code, an underscore (_) at the end of a
       line is used as a line-continuation character. Remove the underscore
       from the end of the line when re-creating this code in Access Basic.
    
           Sub Button182_Click ()
              On Error GoTo CatchBlanks
              Dim Word As Object ' Declare in the module to view the print
                                 ' preview after the Sub ends
    
              DoCmd GoToControl "Photo"
              DoCmd DoMenuItem A_FORMBAR, A_EDIT, A_COPY
              Set Word = CreateObject("Word.Basic")
              Word.FileOpen ("C:\OLEMERGE.DOC")
              Word.EditGoto "Last"
              Word.Insert CStr(Forms![Employees]![Last Name])
              Word.EditGoto "First"
              Word.Insert CStr(Forms![Employees]![First Name])
              Word.EditGoto "Address"
              Word.Insert CStr(Forms![Employees]![Address])
              Word.EditGoto "City"
              Word.Insert CStr(Forms![Employees]![City])
              Word.EditGoto "Region"
              Word.Insert CStr(Forms![Employees]![Region])
              Word.EditGoto "PostalCode"
              Word.Insert CStr(Forms![Employees]![Postal Code])
              Word.EditGoto "Greeting"
              Word.Insert CStr(Forms![Employees]![Last Name])
              Word.EditGoto "Photo"
              Word.EditPaste
              ' Word.FilePrint 0, 0, "0", "", "", "", 0, "1", "", 0, 0, 1, ""
    
              ' NOTE: When sending the current record from Microsoft Access 2.0
              ' to Microsoft Word 95, replace the above line with the
              ' following line:
    
              ' Word.FilePrint 0
    
              ' To send the record directly to the printer, unremark one of the
              ' lines above, and remark the next two lines below.
    
              Word.appmaximize "",1
              Word.FilePrintPreview
              Word.AppActivate "Microsoft Word"
           Exit Sub
    
              CatchBlanks:
                 If MsgBox("Error sending one field, it may be blank. Would _
                 you like to continue?", 52) = 6 Then
                    Resume Next
                 Else
                    Exit Sub
                 End If
    
           End Sub
    
    

  9. Save the form, and then view the form in Form view. Click the new command button.

    Note that the current record is sent to Microsoft Word, merged into the OLEMERGE document, and then opened in print preview.

    Note on output format: Many of the Access data types are output unformatted. (i.e. currency is sent to MS Word as a general number) It is necessary in these cases to format the data manually. This example will format a field called [Price] to output as currency:

    Word.Insert Cstr(Format(Forms![FormName]![Price], “Currency”))


Additional query words: editgoto mail
Keywords : AutoGnrl kbole kbprg PgmHowTo
Version : 2.0 7.0
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.