Rusty
Registered User.
- Local time
- Today, 15:31
- Joined
- Apr 15, 2004
- Messages
- 207
Hey guys,
I have spent a while surfing around the site for the answer to this problem but to no avail.
I have a form with a command button which when pressed opens a Word Document. It's a mail merge template and I want to be able to automate the actual merge (merge to new a new Word document) as soon as the Word document opens.
Any help with this would be greatly appreciated. I am using the code below and cannot get the objWord.MailMerge.Execute line to work.
Rusty
I have spent a while surfing around the site for the answer to this problem but to no avail.
I have a form with a command button which when pressed opens a Word Document. It's a mail merge template and I want to be able to automate the actual merge (merge to new a new Word document) as soon as the Word document opens.
Any help with this would be greatly appreciated. I am using the code below and cannot get the objWord.MailMerge.Execute line to work.
Rusty

Code:
Private Sub cmdOK_Click()
On Error GoTo Err_cmdOK_Click
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
'Dim objWord As Word.Document
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
' Loop through the selected items in the list box and build a text string
For Each varItem In Me!lstTrials.ItemsSelected
strCriteria = strCriteria & ",'" & Me!lstTrials.ItemData(varItem) & "'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select any Trials from the list." _
, vbExclamation, "Nothing to find"
Exit Sub
End If
' Remove the leading comma from the string
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM Trials " & _
"WHERE Trials.[Name of Trial] IN(" & strCriteria & ");"
' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the MailMerge template
Call Shell("""C:\Program Files\Microsoft Office\Office\WINWORD.EXE"" ""O:\ASWCS\Recruitment data\CTU Cancer Trial Monthly Updates\MonthlyUpdateTemplate.doc""", 1)
'objWord.MailMerge.Execute
' Empty the memory
Set db = Nothing
Set qdf = Nothing
Exit_cmdOK_Click:
Exit Sub
Err_cmdOK_Click:
Resume Exit_cmdOK_Click
End Sub