Close Document Failure in Word 2002

R

reitanospa1

Does anyone know why this would fail

Application.DisplayAlerts = wdAlertsNone
ActiveDocument.Close
Application.DisplayAlerts = wdAlertsAll

Most of my experience is in Excel VBA, but it seems like this should
work. What I get is a Save As... dialog and an error that says
"Run-time error '4198': Command failed" when I click the Cancel button.
I'm trying to avoid the dialog box with the dispalyalerts = none, but
it keeps popping up anyway.

When I choose Debug from the error dialog it appears to be hung up on
the .close line.

Is there an alternate way to close a document without being prompted to
save?

Thanks
 
H

Helmut Weber

Hi,

sometimes an additional
activedocument.saved = true
my help.like this:
Application.DisplayAlerts = wdAlertsNone
activedocument.saved = true
ActiveDocument.Close
Application.DisplayAlerts = wdAlertsAll

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
 
J

Jay Freedman

Does anyone know why this would fail

Application.DisplayAlerts = wdAlertsNone
ActiveDocument.Close
Application.DisplayAlerts = wdAlertsAll

Most of my experience is in Excel VBA, but it seems like this should
work. What I get is a Save As... dialog and an error that says
"Run-time error '4198': Command failed" when I click the Cancel
button. I'm trying to avoid the dialog box with the dispalyalerts =
none, but it keeps popping up anyway.

When I choose Debug from the error dialog it appears to be hung up on
the .close line.

Is there an alternate way to close a document without being prompted
to save?

Thanks

Adding either or both of these lines will suppress the dialog:

ActiveDocument.Saved = True

ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

The background is that Word maintains a Boolean property named .Save for
each document in the Documents collection. Any action in the document that
"dirties" it will cause Word to set .Saved = False. This includes editing,
but also things like updating fields, or just displaying the File >
Properties dialog (because that makes Word update the
characters/words/paragraphs/lines count).

When you close the document by any means, manual or programmatic, if .Saved
is False then Word displays the SaveAs dialog. One way to suppress that is
to use the wdDoNotSaveChanges parameter in the Close method; the other way
is to lie to Word and tell it the document isn't dirty.
 

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