Mailmerge word closing question

M

Mike_Gator14

Hi, I have another question with the following code:

stMergeDoc = "E:\Name.doc"

Set objWord = GetObject(stMergeDoc, "Word.Document")

objWord.Application.Visible = True

objWord.MailMerge.OpenDataSource _
Name:=CurrentDb.Name, _
LinkToSource:=True, _
Connection:="QUERY QryName", _
SQLStatement:="Select * from [QryName]"
objWord.MailMerge.Execute

When I use this code, I get 2 word documents, the original 'name.doc'
and a merged document.

I want to close the original, then print the merged document and then
close the merged one also, all without saving.

If InStr(ActiveDocument.Name, "Name") = 0 Then
Set DocResult = Documents(1)
Else
Set DocResult = ActiveDocument
End If

objWord.Close SaveChanges:=wdDoNotSaveChanges (this closes the
original doc)

'print docresult'

DocResult.Close SaveChanges:=wdDoNotSaveChanges (this closes the
merged doc)
Word.Application.Quit (this closes word)

Set objWord = Nothing
Set DocResult = Nothing

If I run this code, the first time everything works fine. But when I
run it immediately again, the DocResult remains empty! If I manually
close the word-docs, everything works again 1 time, but after that
again DocResult = Empty.

Any ideas?

Thanks, Mike
 
C

Cindy M -WordMVP-

Hi Mike_Gator14,
Word.Application.Quit (this closes word)

Set objWord = Nothing
Set DocResult = Nothing

If I run this code, the first time everything works fine. But when I
run it immediately again, the DocResult remains empty! If I manually
close the word-docs, everything works again 1 time, but after that
again DocResult = Empty.
I suspect your problem is shoddy coding. objWord and DocResult no
longer have a container context (the Word application). And you also
don't show us how and where "Word" is defined, nor do you use it
otherwise, when working with these objects. In addition, you use
Documents(1) and ActiveDocument without explicitly stating where
(which application) the code should get these objects from. This means
VBA ends up having to create its own "object" in order to use them,
which never gets released, so the code can't properly release
DocResult.

Better would be something along these lines, always declaring
explicitly what belongs to what:

Dim wdApp as Word.Applicaton
Dim wdDoc as Word.Document
Dim wdDocResult as Word.Document

Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Open stMergeDoc
'do the merge stuff
Set wdDocResult = wdApp.ActiveDocument
'do other stuff, then clean up

wdDoc.close
wdDocResult.Close
wdApp.Quit

Set wdDocResult = Nothing
Set wdDoc = Nothing
Set wdapp = 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