Visual basic help- quitting without saving

I am writing a Visual Basic macro in a Word document and am stuck on one little point. I want to have an icon on my desktop that when I click it, it pops up a dialog box, I fill in a couple of fields in that box, it then inserts what I’ve entered into the document, prints it out, and exits Word. I have everything working fine except for one thing- before it will exit it asks me if I want to save it. I do not want to ever save it, and I don’t want it to ask. Is there a VB command I can use to tell it to exit Word without saving the changes?

Try this:

Application.Quit (False)

The word False is passed as the first optional argument, which is SaveChanges.

Thanks spatterpunk. Actually now I have another problem, that being the fact that there might be other Word documents open when I run this macro, and I don’t want to jeopardize those by quitting Word. Is there a way to quit Word if there are no other documents open, but leave it open if there are?

To answer the next question:

Yes:



If Documents.Count = 1 Then
   Application.Quit SaveChanges:=wdDoNotSaveChanges
Else
   ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
End If


You will notice that I used a different syntax than splatterpunk, which I explain below.

This in fact will work, but there are a couple of problems. SaveChanges takes one of the following values:

wdDoNotSaveChanges
wdPromptToSaveChanges
wdSaveChanges

If you pass False, it will evaluate to 0, which by coincidence is what wdDoNotSaveChanges evaluates to. True evaluates to -1, which is the value of wdSaveChanges. I suppose this is by design, although you can see there is a third choice.

There is also a syntax problem, but it is masked by the fact that you are providing only one argument. You generally cannot use the parentheses unless you are using the “call” keyword.


Call Application.Quit (wdDoNotSaveChanges)

Or you could use


Application.Quit wdDoNotSaveChanges

Or even


Application.Quit SaveChanges:=wdDoNotSaveChanges

They are all exactly equivalent.

The reason that splatterpunk’s code works is that a single value enclosed in parentheses is an expression that evaluates to that value. If you try to use this same syntax using more than one parameters, it won’t compile.

I tacked this code on to the end of the Sub and now it won’t print. The IF/Then argument works correctly though. Any idea why that might be? I tried putting ActiveDocument.Printout inside the If/Then statement but that didn’t work either.

Missed editing window: it acts like perhaps it is quitting Word before the print job has a chance to spool? Sound possible?

Yes, that is possible. If you are using Word conventionally, hit Print, and then immediately Exit, it will tell you that quitting will cancel all print jobs, and ask if you really want to quit. I don’t see a way to wait for print job to finish before you quit, but if I run into something I’ll let you know. The poor man’s solution is to wait for some time interval and hope the print job finished. That is tricky since there is no “sleep” statement in VBA, but it can be done. Let me have a look.

There is a sleep function in Windows that I accessed with this declaration:

Private Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)

and then I put “Sleep 5000” in my program after the print command. It did pause; the only problem is when it goes to sleep it stops processing the print job, then when it wakes up it exits immediately so I’m right back where I started.

Doesn’t


ActiveDocument.PrintOut Background:=False

do this?

Why, yes it does. Thank you.