Jay
Mon Oct 24 08:56:53 CDT 2005
reitanospa1@yahoo.com wrote:
> 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.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:
http://word.mvps.org