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 INFORMATIONStep 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.
- Enter the following in the module's Declarations section:
Option Explicit
Global MergeChan As Integer
Const ID_YES = 6
- 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
- From the Run menu, choose Compile All.
- Save the module as Print Merge, and then close it.
Step Three: Create the Form
- Open the Customers form in Design view.
- Set the form's OnOpen property to:
=Initiate_Word()
- Add a command button with the following properties to the form:
Caption: Print Letter
OnPush: =Send_Record()
- Set the form's OnClose property to:
=Terminate_MergeChan()
- Save the form, and then view it in Form view.
- 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
|