Mail merge with template problem

P

Phill

I am trying to do a mail merge using a stored template (.dot) file. The
problem is that I get two Word documents open, the template and the merge
document. How do I set it up so that I don't get the template document?
This is my code:
Set WordApp = CreateObject("Word.Application")

Set WordDoc = WordApp.Documents.Add(Template:= _
Path & WordTemplate, _
NewTemplate:=False)
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UGhpbGw=?=,

I'm not sure I understand the question/problem. You open a new main merge
document, created from the *.dot file with the code you show us. You may
also get a "Document 1" that's generated automatically when the Word
application starts. And you're saying there's another document window, in
addition? Which version of Word? Does the template itself contain any
macros (especially AutoNew or Document_New)?
I am trying to do a mail merge using a stored template (.dot) file. The
problem is that I get two Word documents open, the template and the merge
document. How do I set it up so that I don't get the template document?
This is my code:
Set WordApp = CreateObject("Word.Application")

Set WordDoc = WordApp.Documents.Add(Template:= _
Path & WordTemplate, _
NewTemplate:=False)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
P

Phill

I get one document that is the template, then another Word instance with the
merge document "Document 1". The user then needs to close the template every
time. I am referencing Word 9 object library.
 
P

Phill

The second one says "Form Letters1"

Phill said:
I get one document that is the template, then another Word instance with the
merge document "Document 1". The user then needs to close the template every
time. I am referencing Word 9 object library.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UGhpbGw=?=,
The second one says "Form Letters1"
Understood. And is the merge being executed by your code, or by the user?

What you describe is normal Word behavior: the main merge document does not close
automatically when a merge is done executing. We can almost surely work out a way
to do this for the user, but I have to know more about how everything functions
(or is supposed to function).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in
the newsgroup and not by e-mail :)
 
P

Phill

Thanks Cindy. The merge is executed from my code. Basically the use clicks
a button, I retrieve the template from my database (it's stored as a blob),
then I create a new Word document using the template, execute the merge.
Here is the merge code:

Public Sub CreateMailMerge()

Dim strFinalDoc As String
Dim WordDoc As Word.Document
Dim WordApp As Word.Application

On Error GoTo Error_CreateProfessionalLetter

strFinalDoc = Path & DocumentName

'-- If the final file is already there, delete it.
On Error Resume Next
Kill strFinalDoc

'If using a stored Word template, then read the template from the table
and write to disk
If WordTemplate = "" Then
Exit Sub
Else
GetStoredTemplate 'Method within my class
End If

On Error GoTo Error_CreateProfessionalLetter

'-- Create the OLE instance of Word, then activate it.
Set WordApp = CreateObject("Word.Application")

Set WordDoc = WordApp.Documents.Add(Template:= _
Path & WordTemplate, _
NewTemplate:=False)

'Set WordDoc = GetObject(strFinalDoc, "Word.Document")
WordDoc.Application.Visible = True

WordDoc.MailMerge.OpenDataSource _
Name:=CurrentDb.Name, _
LinkToSource:=True, _
ReadOnly:=True, _
SQLStatement:="SELECT * FROM tempMailMerge ORDER BY LotNumber", _
subtype:=wdMergeSubTypeWord2000

WordDoc.MailMerge.Execute

Set WordDoc = Nothing

Exit Sub

Error_CreateProfessionalLetter:

Beep
MsgBox "The Following Error has occurred:" & vbCrLf & _
Err.Description, vbCritical, "OLE Error!"
Exit Sub
Resume
End Sub
 
C

Cindy M -WordMVP-

Hi Phil,

OK.

1. You're right to not use GetObject, as in your sample code. It
doesn't work very reliably with Word, and you need the document object
variable - WordDoc - that you initiate using Documents.Add

2. Before you Set WordDoc = Nothing, do WordDoc.close. Look up the
Close method in the Word VBA Help files to check whether you want to
set any of the optional parameters.
Set WordDoc = WordApp.Documents.Add(Template:= _
Path & WordTemplate, _
NewTemplate:=False)

'Set WordDoc = GetObject(strFinalDoc, "Word.Document")
WordDoc.Application.Visible = True

WordDoc.MailMerge.OpenDataSource _
Name:=CurrentDb.Name, _
LinkToSource:=True, _
ReadOnly:=True, _
SQLStatement:="SELECT * FROM tempMailMerge ORDER BY LotNumber", _
subtype:=wdMergeSubTypeWord2000

WordDoc.MailMerge.Execute

Set WordDoc = Nothing

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 

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

Top