PDA

View Full Version : Application.quit without saving



davidboutche
07-27-2009, 03:48 AM
I'm using the command

Application.Quit

to exit word on completion of other tasks. Word of course then asks me if i want to save the document. I don't, I just want it to quit without saving.

How do i do this please?

lucas
07-27-2009, 07:21 AM
See if this helps:
Application.Quit SaveChanges:=wdDoNotSaveChanges

or if you want to save the changes:


Application.Quit SaveChanges:=wdSaveChanges

davidboutche
07-27-2009, 07:35 AM
Thanks Steve, worked perfect.

fumei
07-27-2009, 08:23 AM
Or....close the document - with or without saving - BEFORE quitting the application.

Technically speaking, these are two different things.

If there ARE no documents open - after being saved or not saved first - then Application.Quit will fire properly, as there is no document to ask about.

davidboutche
07-27-2009, 08:28 AM
In this case there would always be document open so regardless of whether I did

activedocument.close

or application.quit it would still need SaveChanges:=wdDoNotSaveChanges after it, yes?

fumei
07-27-2009, 08:40 AM
Ummmm, you have a contradiction.

"there would always be document open"

"application.quit"

If the Application (Word) is Quit, pray tell...how you can have a document open?

ActiveDocument and Application are two different objects, and it is a good thing to think of them (and code them) that way.

davidboutche
07-27-2009, 08:49 AM
OK, if i explain what what I have then you can see where I'm going with it.

There is a document that is open from the start.

Changes are made to it along the way.

If a load of criteria are met the document continues.

If other criteria are met, an email is fired off and the document is no longer required and email takes over the process.

In the latter process the intention was to close any open documents and close word.

I was using the application quit command to kill two birds with one stone.

Are you suggesting it would be better practice to close the document with donotsavechanges and the application.quit?

Thanks for all the help by the way... i'm learning.. a lot!

fumei
07-27-2009, 09:08 AM
"I was using the application quit command to kill two birds with one stone.

1. No you are not. There are TWO birds - objects. The Document object, and the Application object. Both must be dealt with. Yes, it is true the following will work, and it looks like one stone, but it is not:
[/vba]
Application.Quit SaveChanges:=wdDoNotSaveChanges
[/vba]It works because VBA is being nice and polite. It is, in fact, performing actions (instructions) on the two objects.

VBA parses the above, sees the SaveChanges parameter, actions the parameter against the DOCUMENT object, then actions .Quit against the APPLICATION object.

"Are you suggesting it would be better practice to close the document with donotsavechanges and the application.quit?"

Yes, yes, yes....and again YES! I am indeed suggesting that. It is always best practice to deal with objects explicitly. True, sometimes VBA can be rather nice and polite. However.....

If you ever use VBA code across applications - the most common is probably interactions between Word and Excel - it can be vital, critical, that you deal with objects explicitly. So, yes, it is best practice to try and understand what is really going on.

Application and ActiveDocument (or any Document object) are two different objects. Deal with them as such.

For example, I have seen (a number of times) code that created the Application, actioned a document, closed the document, Quit the Application....created the Application, actioned the next document, closed that document, Quit the Application....and on and on.

The person associated Application = Document, so for each document they were processing (a bunch in a folder), they created, used, Quit, the Application.

This is BAD BAD BAD. Not only is it inefficient, but it is just asking for memory management problems.

Word is very very good at processing documents. ONE instance of Application, can handle many many different Documents.

Why? Because they are different objects.