Microsoft KB Archive/99570

{|
 * width="100%"|

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

 * }

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.

Step One: Create the Word for Windows Document
 Start Word for Windows and open a new document.  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.  Save this document as C:\DDEMERGE.DOC. 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. 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
 Start Microsoft Access and open the sample database NWIND.MDB. Create a new module.</li> Enter the following in the module's Declarations section:

Option Explicit

Global MergeChan As Integer

Const ID_YES = 6</li>  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.

<pre class="CODESAMP">     Function Initiate_Word Dim Chan As Variant Dim WordTopics As Variant Chan = StartApp(&quot;Winword&quot;, &quot;System&quot;) '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, &quot;Topics&quot;)

If InStr(1, WordTopics, &quot;DDEMERGE.DOC&quot;) = 0 Then DDEExecute Chan, &quot;[FILEOPEN &quot;&quot;C:\DDEMERGE.DOC&quot;&quot;]&quot; End If

DDETerminate Chan Mergechan = DDEInitiate(&quot;Winword&quot;, &quot;C:\DDEMERGE.DOC&quot;)

Exit Function

AlertUser: MsgBox &quot;MS Access is unable to initiate a DDE _ channel with the document DDEMERGE.DOC&quot; Resume Next End Function

Function Send_Record

On Error GoTo CatchBlanks

DDEPoke Mergechan, &quot;CompanyName&quot;,_ Forms![Customers]![Company Name] DDEPoke Mergechan, &quot;ContactName&quot;, _ Forms![Customers]![Contact Name] DDEPoke Mergechan, &quot;Address&quot;, _ Forms![Customers]![Address] DDEPoke Mergechan, &quot;City&quot;, _ Forms![Customers]![City] DDEPoke Mergechan, &quot;Region&quot;, _ Forms![Customers]![Region] DDEPoke Mergechan, &quot;PostalCode&quot;, _ Forms![Customers]![Postal Code]

DDEExecute Mergechan, &quot;[FilePrint]&quot;

Exit Function

CatchBlanks: If MsgBox(&quot;Error sending one field, it may be blank._              Would you like to continue?&quot;, 52) = ID_YES Then Resume Next Else Exit Function End If     End Function

Function Terminate_MergeChan

DDETerminate MergeChan

End Function </li> From the Run menu, choose Compile All.</li> Save the module as Print Merge, and then close it.</li></ol>

Step Three: Create the Form

 * 1) Open the Customers form in Design view.
 * 2) Set the form's OnOpen property to:

=Initiate_Word
 * 1) Add a command button with the following properties to the form:

Caption: Print Letter

OnPush: =Send_Record
 * 1) Set the form's OnClose property to:

=Terminate_MergeChan
 * 1) Save the form, and then view it in Form view.
 * 2) 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.