Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot Automate close of Word document if Word is already open

Status
Not open for further replies.

AlexBeet

IS-IT--Management
Aug 2, 2001
12
GB
The following code is a snapshot of a simple mailmerge from an access 2000 database called lord2.mdb.
This routine works perfectly well if Word is NOT already open but if it is already open , I get a dialog box asking me if I wish to save the changes to the original mail merge document (Agent-Blank.doc) when I try to close it (objWord5.Close SaveChanges:=wdDoNotSaveChange).
Can any one tell me why, if Word is already open , the wdDoNotSaveChange argument is not working?
If Word is not already open, the original mailmerge document (Agent-Blank.doc) closes without any dialog box and leaves me with a new document showing my required data.
Any tips on how to over come this would be appreciated.

Private Sub SendToMerge_Click()
Dim DocToBeUsed As String
Dim QueryToBeUsed As String
Dim objWord5 As Word.Document
DocToBeUsed = "C:\My Documents\Mailmerge\Agent-Blank.doc"
DoCmd.OpenForm "Case", acNormal, , "[CaseNum] = Forms!printanydoc!CaseNum", acFormReadOnly, acHidden

QueryToBeUsed = "printanydoc-case"

Set objWord5 = GetObject(DocToBeUsed, "Word.Document")

objWord5.Application.Visible = True
objWord5.MailMerge.OpenDataSource _
Name:="c:\program files\lla\lord2.mdb", _
LinkToSource:=True, _
Connection:="QUERY " & QueryToBeUsed, _
SQLStatement:="SELECT * FROM " & QueryToBeUsed & " ;"

objWord5.MailMerge.Destination = wdSendToNewDocument
objWord5.MailMerge.Execute
objWord5.Close SaveChanges:=wdDoNotSaveChanges
DoCmd.Close acForm, "Case", acSaveNo
End Sub
 
I have a related/weird problem... any ideas....
Help! If you can.... I'd really appreciate it.

Problem: I'm trying to run some Access97 code that kicks of a MailMerge in Word. The following line seems to hang (e.g. Word consumes 98% of CPU), until, and get this, you move the mouse around. Moving the mouse around snaps Word out of its loop. Also, if you hold the Shift key down when you click on the button to start the process, that will stop Word from going into this processing loop.

code = "Set ThisWord = GetObject(strdocfile & ".doc", "Word.Document")

ThisWord is previously defined as "Dim ThisWord As Word.Document", and strdocfile is simply a string (e.g. unc path with file name, excluding the doc extension).

Thanks in advance
 
I don't have an answer to your question, sorry. But I am curious, is this code written in your Ms Acess VBA or in Word VBA?
Thanks
 
I have found a solution - Just add
objWord5.Application.DisplayAlerts = False
before closing the document - seems to work ok.

cscdesktop - Sorry, I can only guess reasons why this occurs - Is the mailmerge you are performing very large? (Lots of pages?). Have you monitored the CPU usage with your mailmerge using other sources to see if this is the norm on your system? Does your merge involve an access query with a lot of calulation? (Just a few possibilities)

mcbigj - The code is written in Access VBA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top