ACC: Sending the Current Record to Word with OLE Automation

ID: Q124862


The information in this article applies to:
  • Microsoft Access versions 2.0, 7.0

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


SUMMARY

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:

Q131583 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:


  2. 
          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 
  3. 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.


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


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


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


  7. Open the Employees form in Design view.


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


  9. 
    Dim Word As Object 
  10. Add a command button to the form and set the button's OnClick property to the following event procedure.


  11. 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 
  12. 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. (that is, currency is sent to MS Word as a general number) It is necessary in these cases to format the data manually. The following example will format a field called [Price] to output as currency:
    Word.Insert Cstr(Format(Forms![FormName]![Price], [ASCII 147]Currency[ASCII 148]))


Additional query words: editgoto mail

Keywords : kbole kbprg kbdta AccCon KbVBA
Version : WINDOWS:2.0,7.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: August 2, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.