ACC1x: Sending the Current Record to Word for Windows with DDE

ID: Q99570


The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1


SUMMARY

This article describes how to send the current record in Microsoft Access version 1.x to Microsoft Word version 2.0 or 6.0 for Windows to be merged into a prewritten letter and printed.


This article assumes that you are familiar with dynamic data exchange (DDE), setting bookmarks in Word for Windows, and creating modules and using Access Basic in Microsoft Access.


MORE INFORMATION

Step One: Create the Word for Windows Document


  1. Start Word for Windows and open a new document.


  2. Enter the following in the blank document:
    
          CompanyName
          Address
          City, Region, PostalCode
          Country
    
          Dear ContactName,
    
             Northwind Traders would like to thank you for
             your business during the past year. Enclosed
             you will find several samples of new products
             that we are excited to announce.
    
          Sincerely,
          Northwind Traders. 


  3. Save this document as C:\DDEMERGE.DOC.


  4. To create the bookmarks, select CompanyName, and choose Bookmark from the Insert menu in Word 2.0, or from the Edit menu in Word 6.0. Name the bookmark CompanyName.


  5. Repeat step 4 to create bookmarks for each of the fields (Address, City, Region, PostalCode, Country, and ContactName). Make sure to give each bookmark the appropriate name.


Step Two: Create the Access Basic Module


  1. Start Microsoft Access and open the sample database NWIND.MDB.


  2. Create a new module.


  3. Enter the following in the module's Declarations section:

    Option Explicit
    Global MergeChan As Integer
    Const ID_YES = 6


  4. Enter the following functions in the module:

    NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore when re-creating this code in Access Basic.
    
          Function Initiate_Word ()
             Dim Chan As Variant
             Dim WordTopics As Variant
             Chan = StartApp("Winword", "System")
             'StartApp is already in the NWIND database. To find it,
             'Choose Modules, then Design in the Database Window.
             'Choose StartUp from the Procedure list box
    
             On Error GoTo AlertUser:
             WordTopics = DDERequest(Chan, "Topics")
    
             If InStr(1, WordTopics, "DDEMERGE.DOC") = 0 Then
               DDEExecute Chan, "[FILEOPEN ""C:\DDEMERGE.DOC""]"
             End If
    
             DDETerminate Chan
             Mergechan = DDEInitiate("Winword", "C:\DDEMERGE.DOC")
    
          Exit Function
    
          AlertUser:
             MsgBox "MS Access is unable to initiate a DDE _
                  channel with the document DDEMERGE.DOC"
             Resume Next
          End Function
    
          Function Send_Record ()
    
             On Error GoTo CatchBlanks
    
             DDEPoke Mergechan, "CompanyName",_
                Forms![Customers]![Company Name]
             DDEPoke Mergechan, "ContactName", _
                Forms![Customers]![Contact Name]
             DDEPoke Mergechan, "Address", _
                Forms![Customers]![Address]
             DDEPoke Mergechan, "City", _
                Forms![Customers]![City]
             DDEPoke Mergechan, "Region", _
                Forms![Customers]![Region]
             DDEPoke Mergechan, "PostalCode", _
                Forms![Customers]![Postal Code]
    
             DDEExecute Mergechan, "[FilePrint]"
    
             Exit Function
    
          CatchBlanks:
             If MsgBox("Error sending one field, it may be blank._
                   Would you like to continue?", 52) = ID_YES Then
               Resume Next
             Else
               Exit Function
             End If
          End Function
    
          Function Terminate_MergeChan ()
    
             DDETerminate MergeChan
    
          End Function 


  5. From the Run menu, choose Compile All.


  6. Save the module as Print Merge, and then close it.


Step Three: Create the Form


  1. Open the Customers form in Design view.


  2. Set the form's OnOpen property to:

    =Initiate_Word()


  3. Add a command button with the following properties to the form:

    Caption: Print Letter
    OnPush: =Send_Record()


  4. Set the form's OnClose property to:

    =Terminate_MergeChan()


  5. Save the form, and then view it in Form view.


  6. Choose the Print Letter button on the form. The current customer record will be sent to Word for Windows, merged into the DDEMERGE.DOC document, and printed.



REFERENCES

Microsoft Access "Introduction to Programming," versions 1.0 and 1.1, Chapter 9, "Dynamic Data Exchange"

Additional query words: merge printmerge browse

Keywords : kbinterop
Version : 1.0 1.1
Platform : WINDOWS
Issue type : kbinfo


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