Word mail merge code problem

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
 
C

Cheese_whiz

Hi Ken,

I think if I were you I'd plug in a valid path to a merge document in place
of the strFilePath variable in your GetObject function (with parens, of
course).

If it works, then you know you aren't getting the full path with parens
using your variable.

I could be right, but I wouldn't bet anything more than a hot dog on it.

CW
 
K

Ken Warthen

CW,

Thanks for the suggestion. I tried hardcoding the path to the Word document
in the line:

Set objWord = GetObject("H:\DSi Project\Other Files\DSi Letter
Template.docx", "Word.Document")

Unfortunately, I get the same result; the system completely hangs.

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top