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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to stop word from asking "Do you want to save changes" 1

Status
Not open for further replies.

ordendelfai

Technical User
Nov 8, 2002
187
US
Happy new years everyone!! =)

I am using the below code to do mail merges, and need to find a way to stop word from asking "do you want to save the changes you made to "Form Letters 1" after this code

If WordWasNotRunning = False Then
objWord.Application.Quit
End If

This code makes Word close alright, but the save question is annoying as we do not save our mailmerges.


Thanks =D






Public Function MergeIt1Employee()
Dim objWord As Word.Document
Set objWord = GetObject("S:\Delphi\Templates\ExternalCorrespondence\1EmployeeTermGroup.doc", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the Access database.
objWord.MailMerge.OpenDataSource _
Name:="C:\Delphi\Delphi.mdb", _
LinkToSource:=False, _
Connection:="TABLE tblMailMerge"
' Execute the mail merge.
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
'The following line must follow the Execute statement because the
'PrintBackground property is available only when a document window is
'active. Without this line of code, the function will end before Word
'can print the merged document.
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
If WordWasNotRunning = False Then
objWord.Application.Quit
End If

End Function
 
Try this:

Code:
objWord.Application.ActiveDocument.PrintOut
Code:
objWord.Application.ActiveDocument.Saved = True
Code:
If WordWasNotRunning = False Then
objWord.Application.Quit


HTH
Mike
 
Nope, when the Word object opens, and after it merges, then prints, the word application just hangs with an hour glass forever. I tried putting into the If statement, and after the IF statement, and the same result (application hangs) occurs.
 
Application.DisplayAlerts = False

works in Excel VBA

Remember to reset to true.
 
Darn, I tried:

objWord.Application.DisplayAlerts = False

And it didn't hang, but it didn't stop the save alert popup.

I am running the code from Access 97 to merge data into a Word 97 document.

Thanks for trying ;o)
 
ordendelphai,

I mocked this up using your code, a form letter in word and set up mail merge fields using and Access 97 database table as data source. When I first ran this, Word prompted me to save both "Form Letters 1" and my original form letter doc. Inserting the following lines eliminated both prompts and closed Word:

Code:
objWord.Application.ActiveDocument.Saved = True
(same as I posted previously)
Code:
objWord.Saved = True

Do you receive two prompts? Perhaps try the above.


Regards,
Mike
 
Hmm, maybe I am inserting them in the wrong location. Where exactly are your inserting the code.

Thanks for researching this so much ;o)
 
Wait, YAY THAT WORKED!!!

Adding:
objWord.Saved = True

is what did it. For some wierd reason just having:

objWord.Application.ActiveDocument.Saved = True

by itself caused word to hang. But both together closes immediately. THANKS AGAIN!!! =D

 
Add this to your code after the mail merge:

ActiveDocument.Close savechanges:=wdDoNotSaveChanges

 
RobCPA,

Yeah, I think that would work also. I always forget about that construct (don't do much programming in Word).

The issue tripping up ordendelphai was that Word prompts to save both the new doc created from the merge and also the original doc that contains the merge fields. Therefore, I think your suggestion, extended slightly, should also do the trick:

Code:
objWord.Application.ActiveDocument.Close savechanges:=wdDoNotSaveChanges
objWord.Close savechanges:=wdDoNotSaveChanges

btw, I ran the mail merge code from Excel (one VBA container's as good as another [wink])

Regards,
Mike
 
Mike,

You're right about needing to close both the original and the new docs. In my apps, I would set the original document before merge like:

Dim docorig as Word.document
Dim docnew as Word.document

Set docorig = ObjWord.ActiveDocument

then set the merged document after merge like:

Set docnew = ObjWord.ActiveDocument

then to close:

docorig.Close (0)
docnew.close (0) OR since the original is already closed due to the code above, then use the ActiveDocument.Close savechanges:=wdDoNotSaveChanges

I haven't programmed much in Word VBA either, but the above works well for my app.

Rob





 
This has been working well so far in my testing, but ran across one problem that is probably really easy to fix, but I don't know the code.

If I already have Word open, then this code uses that Word instance to do the mail merge, and closes it.

I need to add something into the code that makes the merge happen everytime in a new instance of Word.

Thanks again for any help =)

Joel
 
Just to update, I changed my mind. I don't need it to open in a new document, just have the merged document only close (and not try to close the other word apps).

ActiveDocument.Close savechanges:=wdDoNotSaveChanges

Just like that worked perfectly :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top