.TITLE: INF: Merging Records To Word For Windows Via DDE
.VERSION(S): 1.00
.OPERATING SYSTEM(S): WINDOWS

-----------------------------------------------------------
The information in this article applies to:

 - Microsoft Access version 1.0
-----------------------------------------------------------

Summary:

This article describes the steps for creating a form which 
allows the user to press a button to send the current record 
to Microsoft Word for Windows. The data sent is merged into 
a pre-written letter and printed.

The article assumes that you understand Dynamic Data 
Exchange, setting bookmarks in Word for Windows, and 
creating modules in Microsoft Access. 

More Information:

Step One: Create the Winword Document
-------------------------------------
1) Start Winword and open a new document.

2) Type in the following:

 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 DDEMERGE.DOC.

4) To create the bookmarks, highlight CompanyName and
   choose Bookmark from the Insert menu.  Name the 
   Bookmark "CompanyName", without quotes.

5) Repeats these steps, creating bookmarks for the 
   fields: Address, City, Region, PostalCode, Country,
   and ContactName.


Step Two: Create The Access Basic Modules
=========================================

1) Open the example database NWIND.MDB. (One of 
   the following modules uses the function STARTAPP()
   which is located in the module Introduction to 
   Programming.

2) Create a new module called Print Merge.

3) Place the following statement in the (declarations) 
   section:
  Dim Mergechan As Integer  

4) Create a new function called Initiate_Word () 

   Function Initiate_Word ()
      Chan = StartApp("Winword", "System")

      On Error GoTo AlertUser:
      WordTopics = DDERequest(Chan, "Topics")

      If InStr(1, WordTopics, "DDEMERGE.DOC") = 0 Then
        DDEExecute Chan, "[FILEOPEN(""DDEMERGE.DOC"")]"
      End If

      DDETerminate Chan
      Mergechan = DDEInitiate("Winword", "DDEMERGE.DOC")

   Exit Function

   AlertUser:
      MsgBox "Access is unable to initiate a DDE 
   channel with the document DDETEST.DOC"
      Resume Next 
   End Function    

5) Create a new function called Send_Record()

   Function Send_Record ()
 
     Dim Chan
     Dim ControlName As Control
     Dim BookMarks As String
  
     On Error GoTo CatchBlanks:

     DDEPoke Mergechan, "CompanyName", 
   Forms![Customers DDE]![Company Name]
     DDEPoke Mergechan, "ContactName", 
   Forms![Customers DDE]![Contact Name]
     DDEPoke Mergechan, "Address", 
   Forms![Customers DDE]![Address]
     DDEPoke Mergechan, "City", 
   Forms![Customers DDE]![City]
     DDEPoke Mergechan, "Region", 
   Forms![Customers DDE]![Region]
     DDEPoke Mergechan, "PostalCode", 
   Forms![Customers DDE]![Postal Code]
   
     DDEExecute Mergechan, "[FilePrint]"
    
     Exit Function

   CatchBlanks:
      If MsgBox("One of these fields is blank. Would
    you like to continue?", 52) = 6 Then
        Resume Next
      Else
        Exit Function
      End If
   End Function

Note: Each of the DDEPoke statements should be on one
      line in your function.  They are split in this
      article for readability.

6) Create a function called Terminate_MergeChan():

   Function Terminate_MergeChan ()

    DDETerminate MergeChan

   End Function


7) Choose Compile All from the Run menu and then
   close and save the module.


Step Three: Create the Form
=========================================
1) Open the form [Customers] in design mode.

2) Set the OnOpen property of the form to:
 =Initiate_Word()

3) Set the OnClose property of the form to:
 =Terminate_MergeChan()

4) Add a new button to the Customers form.

5) Set the Caption property of the button to:
 Print Letter.

6) Set the OnPush property of the button to:
 =Send_Record()

7) Save the form and switch to browse mode. Click on the
   Print Letter button. The current record will be sent
   to Word for Windows, merged into the document 
DDEMERGE.DOC
   and then printed.


Reference(s):
Introduction to Programming, Chapter 9 Dynamic Data 
Exchange.
Word for Windows Technical Reference pp. 20-25
