K
Ken Warthen
I'm using the following code to merge a record from my Access 2007 database
to a Word 2007 mail merge document.
'Run the mail merge code, saving the document to the specified
'project docs folder
Dim strFilePath As String
Dim objWordApp As Word.Application
Dim objWord As Word.Document
Dim oSel As Word.Selection
'Get the mail merge template
With Application.FileDialog(msoFileDialogOpen)
.Title = "Select Word Document"
.AllowMultiSelect = False
.Filters.Add "Word documents", "*.docx; *.doc,1"
.Filters.Add "All Files", "*.*", 2
'Show the dialog and if the dialog returns
'True, then open the selected document
If .Show = True Then
strFilePath = .SelectedItems(1)
Else
MsgBox "A Word document was not selected! Choose a mail merge
document.", vbOKOnly + vbInformation, "Invalid Selection"
Exit Sub
End If
End With
'Create the Word instance and make it visible
Set objWord = GetObject(strFilePath, "Word.Document")
objWord.Application.Visible = True
'Open the datasource
objWord.MailMerge.OpenDataSource _
Name:=Application.CurrentProject.FullName, _
OpenExclusive:=False, _
LinkToSource:=True, _
Connection:="qryMailMerge", _
SQLStatement:="SELECT * FROM qryMailMerge;"
'Execute the mail merge
objWord.MailMerge.Execute
objWord.Close (0)
Set objWord = Nothing
When I get to the line: Set objWord = GetObject(strFilePath,
"Word.Document") the application hangs and I have to use Windows Task Manager
to kill the WINWORD.EXE process. I double checked to insure I have Microsoft
Word 12.0 Object Library as a selected reference. Any idea on why this is
happening?
Ken
to a Word 2007 mail merge document.
'Run the mail merge code, saving the document to the specified
'project docs folder
Dim strFilePath As String
Dim objWordApp As Word.Application
Dim objWord As Word.Document
Dim oSel As Word.Selection
'Get the mail merge template
With Application.FileDialog(msoFileDialogOpen)
.Title = "Select Word Document"
.AllowMultiSelect = False
.Filters.Add "Word documents", "*.docx; *.doc,1"
.Filters.Add "All Files", "*.*", 2
'Show the dialog and if the dialog returns
'True, then open the selected document
If .Show = True Then
strFilePath = .SelectedItems(1)
Else
MsgBox "A Word document was not selected! Choose a mail merge
document.", vbOKOnly + vbInformation, "Invalid Selection"
Exit Sub
End If
End With
'Create the Word instance and make it visible
Set objWord = GetObject(strFilePath, "Word.Document")
objWord.Application.Visible = True
'Open the datasource
objWord.MailMerge.OpenDataSource _
Name:=Application.CurrentProject.FullName, _
OpenExclusive:=False, _
LinkToSource:=True, _
Connection:="qryMailMerge", _
SQLStatement:="SELECT * FROM qryMailMerge;"
'Execute the mail merge
objWord.MailMerge.Execute
objWord.Close (0)
Set objWord = Nothing
When I get to the line: Set objWord = GetObject(strFilePath,
"Word.Document") the application hangs and I have to use Windows Task Manager
to kill the WINWORD.EXE process. I double checked to insure I have Microsoft
Word 12.0 Object Library as a selected reference. Any idea on why this is
happening?
Ken