"Sending The Current Record to WinWord via DDE"

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,(DDE), 
setting bookmarks in Word for Windows, and creating modules in Microsoft 
Access. 

Additionally, in this example the placement of the file DDEMERGE.DOC is 
important.  You will find it easier to test if you open the file in Word
before opening the Customers form.  You may also want to include code 
that changes the Word default directory to the directory that the 
DDEMERGE.DOC file resides in.

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.

Note: WinWord will fail if the field names contain spaces when attempting to complete the merge.  
Plus if you copy the text into a WinWord document, be sure to remove the tabs.

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

(One of the following modules uses the function STARTAPP() which is located in the module 
Introduction to Programming.

Also, each of the Access Basic statements should be on one line in your function.  
Some lines are split in this article for readability.)

1) Open the example database NWIND.MDB.

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 ()
   Dim Chan As Variant
   Dim WordTopics As Variant     
   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 DDEMERGE.DOC"
      Resume Next 
   End Function    

5) Create a new function called Send_Record()
   Function Send_Record ()
 
     Dim Chan As Variant
     Dim ControlName As Control
     Dim BookMarks As String
  
     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]
     DDEPoke MergeChan, "Country", Forms![Customers]![Country]
   
     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

6) Create a function called Terminate_MergeChan():
   Function Terminate_MergeChan ()
      On Error Resume Next
      DDETerminate MergeChan
   End Function

7) Choose Compile All from the Run menu and then close and save the 
module.  If you receive any error messages during this step, you 
should examine the code that you entered for possible mistakes.


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.
