Consulting

Results 1 to 8 of 8

Thread: Application.quit without saving

  1. #1

    Application.quit without saving

    I'm using the command

    [VBA]Application.Quit[/VBA]

    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?

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    See if this helps:
    [VBA]Application.Quit SaveChanges:=wdDoNotSaveChanges[/VBA]

    or if you want to save the changes:

    [VBA]
    Application.Quit SaveChanges:=wdSaveChanges
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Thanks Steve, worked perfect.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  5. #5
    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?

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  7. #7
    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!

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •