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

How to close a Word doc w/o saving through VB

Status
Not open for further replies.

hrm03

Programmer
Apr 30, 2003
28
US
I am trying to close a Word Doc through VB without saving it and its just not working. Through VB I have modified this document and printed it out, but I would like it to revert back to the original after it is printed and not save the modifications. I have tried making the file read only, but I keep getting an error message with that. Any help would be appreciated.
 
hrm03,

You have to set the document's 'Saved' property to true, tricking Word into thinking it has already been saved. Then, turn off your warnings momentarily, close the document preserving its original state, then turning your warnings back on. Hope this helps..



Sub closewithoutsave()
Application.DisplayAlerts = False
ActiveDocument.Saved = True
ActiveDocument.Close
Application.DisplayAlerts = True

End Sub

"It's more like it is now, than it ever has been."
 
Hi
Code:
ActiveDocument.Close False
should do the trick. You may need to change the object from ActiveDocument to suit your needs.

If this doesn't help you may need to post some additional info such as error message or are you using VBA or a VB app.
;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 
Hi hrm03,

Please don't double post. See my response in the VBA forum - thread707-612093 - the correct place for VBA queries.

Enjoy,
Tony
 
danomaniac - Thanks but I have tried that and get the following error message : "Invalid use of property"(.saved)

Loomah - I have also tried your approach, but it will still save the modifications that were made. I am using a VB app for this particular project.

I am trying to print the document with the modifications before closing it. Does that matter any? The user has no options here, it automatically prints and the Word application never opens. Thanks for trying.
 
Hi hrm03,

I'm not a VB programmer but something still doesn't add up here. the Word application never opens? How are the changes made? How is the document printed?

Enjoy,
Tony
 
I'm sorry Tony, I have replied to your other posting in the VB area with more detail.
 
hrm - That's odd. I had no trouble using the code I posted. What version of Word are you using? I don't know if it makes a difference or not. I'm using Office 2000 Pro.

"It's more like it is now, than it ever has been."
 
I am also using Office 2000. From what everyone says that code should work. I am at a loss as to why it doesn't.
 
Have you tried running the code on a different document just to see if it works? If it does, then it's your document, not the code. - Perhaps you are modifying the document in a way that you can't set the saved property to true? I can't imagine how, but it's worth a try.

Hmmm.

"It's more like it is now, than it ever has been."
 
THANK YOU. I don't know what it was, but I went ahead and re-created the document I was trying to update and it WORKED! Thanks for the help!! [smile2]
 
Glad to help.
Sometimes when all else fails, drop back and punt.

"It's more like it is now, than it ever has been."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top