Chapter 22: Integrating Access with Other Applications
803
FIGURE 22.7
After a successful merge, all bookmarks have been replaced with their respective data.
When the user clicks the Print Thank-You Letter button on the Orders form, Word generates a
thank-you letter with all the pertinent information. The following code shows the MergetoWord
function in its entirety so you can see in-depth how it works.
Public Sub MergeToWord()
‘ This method creates a new Word
‘ document using Automation.
On Error Resume Next
Dim rsCust As New ADODB.Recordset
Dim sSQL As String
Dim WordObj As Word.Application
Dim iTemp As Integer
sSQL = “SELECT * FROM Customers “ _
& “WHERE CustomerNumber = “ _
& Forms!Orders![CustomerNumber]
rsCust.Open sSQL, CurrentProject.Connection
If rsCust.EOF Then
MsgBox “Invalid customer”, vbOKOnly
Exit Function
End If
DoCmd.Hourglass True
‘Try to get a running instance of Word:
Set WordObj = GetObject(, “Word.Application”)
If Err.Number <> 0 Then