Close without Saving Changes

S

Steve C

I'm looking for code that will not save changes when a user closes a document
called Seating Chart.doc. I've already made it read-only to protect it, but
for simplicity, I just want it to close without the user being prompted to
save changes when either the close button or File > Close is chosen. I've
tried the following lines of code, but all still prompt the user to save
changes. Thanks!

Private Sub Document_Close()
ActiveDocument.Close SaveChanges:=False
ActiveDocument.Quit Savechanges:=False
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
End Sub
 
P

Perry

Look in help for the the .Saved() property/state of a document.
This statement is what y're looking for

ActiveDocument.Saved = True

--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 
L

Lene Fredborg

Set the Saved property to true. This should do what you want:

ActiveDocument.Saved = True

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
H

Helmut Weber

Hi Lene,

and why does the following not work,
here and now, without error-handling?

Private Sub document_close()
' On Error Resume Next, ok with it
ActiveDocument.Saved = True
ActiveDocument.Close ' command failed without on error
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
L

Lene Fredborg

Hi Helmut,

I am quite sure that I don't know enough about how the document_close event
actually works to answer your question precisely. I think that the problem is
that when the event is triggered, the document is already in the process of
being closed - therefore, you are not supposed to/allowed to include a close
as part of the event. The macro works (i.e. the document is closed without
being saved and without a prompt) with _only_ this code line:

ActiveDocument.Saved = True

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
D

Doug Robbins - Word MVP

Just use

ActiveDocument.Close wdDoNotSaveChanges

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Russ

What everyone is saying is to use:
ActiveDocument.Close wdDoNotSaveChanges
By **itself** in a regular macro to close a document without prompting.

But if you a using the 'close event' response to a close message, according
to Lene use only:

Private Sub Document_Close()
ActiveDocument.Saved = True 'to close without prompt
End Sub

Because reading this 'close event' means it has already read a message to
close and repeating the document close command within the 'close event'
might raise an error.
 
H

Helmut Weber

Hi Russ,

excellent observation, very true.

In a regular macro I've encountered cases where both

ActiveDocument.Saved = True
plus
ActiveDocument.Close wdDoNotSaveChanges

were required.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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