# Visual basic help- quitting without saving

**URL:** <https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866>\
**Category:** Factual Questions\
**Created:** [June 13, 2007, 4:04pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866 "2007-06-13T16:04:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Don\_t\_Call\_Me\_Shirley](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@Don\_t\_Call\_Me\_Shirley](https://boards.straightdope.com/u/Don_t_Call_Me_Shirley)\
**Post date:** [June 13, 2007, 4:04pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866/1 "2007-06-13T16:04:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![splatterpunk](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@splatterpunk](https://boards.straightdope.com/u/splatterpunk)\
**Post date:** [June 13, 2007, 4:50pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866/2 "2007-06-13T16:50:46Z")

</div>

[QUOTE=Don’t Call Me Shirley]  
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?  
[/QUOTE]

Try this:

Application.Quit (False)

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

---

<div class="post-metadata">

**Author:** ![Don\_t\_Call\_Me\_Shirley](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@Don\_t\_Call\_Me\_Shirley](https://boards.straightdope.com/u/Don_t_Call_Me_Shirley)\
**Post date:** [June 13, 2007, 5:09pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866/3 "2007-06-13T17:09:18Z")

</div>

[QUOTE=splatterpunk]  
Try this:

Application.Quit (False)

The word False is passed as the first optional argument, which is SaveChanges.  
[/QUOTE]

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?

---

<div class="post-metadata">

**Author:** ![CookingWithGas](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cookingwithgas/32/485_2.png) [@CookingWithGas](https://boards.straightdope.com/u/CookingWithGas)\
**Post date:** [June 13, 2007, 6:06pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866/4 "2007-06-13T18:06:17Z")

</div>

To answer the next question:

> [@](#):
>
> …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?

Yes:

```auto

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.

[QUOTE=splatterpunk]  
Try this:

Application.Quit (False)

The word False is passed as the first optional argument, which is SaveChanges.  
[/QUOTE]  
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.

```auto

Call Application.Quit (wdDoNotSaveChanges)

```

Or you could use

```auto

Application.Quit wdDoNotSaveChanges

```

Or even

```auto

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.

---

<div class="post-metadata">

**Author:** ![Don\_t\_Call\_Me\_Shirley](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@Don\_t\_Call\_Me\_Shirley](https://boards.straightdope.com/u/Don_t_Call_Me_Shirley)\
**Post date:** [June 13, 2007, 7:19pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866/5 "2007-06-13T19:19:35Z")

</div>

> [@](#):
>
> If Documents.Count = 1 Then  
> Application.Quit SaveChanges:=wdDoNotSaveChanges  
> Else  
> ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges  
> End If

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.

---

<div class="post-metadata">

**Author:** ![Don\_t\_Call\_Me\_Shirley](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@Don\_t\_Call\_Me\_Shirley](https://boards.straightdope.com/u/Don_t_Call_Me_Shirley)\
**Post date:** [June 13, 2007, 7:29pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866/6 "2007-06-13T19:29:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CookingWithGas](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cookingwithgas/32/485_2.png) [@CookingWithGas](https://boards.straightdope.com/u/CookingWithGas)\
**Post date:** [June 13, 2007, 7:48pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866/7 "2007-06-13T19:48:09Z")

</div>

[QUOTE=Don’t Call Me Shirley]  
Missed editing window: it acts like perhaps it is quitting Word before the print job has a chance to spool? Sound possible?  
[/QUOTE]  
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.

---

<div class="post-metadata">

**Author:** ![Don\_t\_Call\_Me\_Shirley](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@Don\_t\_Call\_Me\_Shirley](https://boards.straightdope.com/u/Don_t_Call_Me_Shirley)\
**Post date:** [June 14, 2007, 3:05pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866/8 "2007-06-14T15:05:58Z")

</div>

> [@CookingWithGas](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![larsenmtl](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@larsenmtl](https://boards.straightdope.com/u/larsenmtl)\
**Post date:** [June 14, 2007, 7:45pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866/9 "2007-06-14T19:45:32Z")

</div>

[QUOTE=CookingWithGas]  
I don’t see a way for print job to finish before you quit, but if I run into something I’ll let you know.  
[/QUOTE]

Doesn’t

```auto

ActiveDocument.PrintOut Background:=False

```

do this?

---

<div class="post-metadata">

**Author:** ![Don\_t\_Call\_Me\_Shirley](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@Don\_t\_Call\_Me\_Shirley](https://boards.straightdope.com/u/Don_t_Call_Me_Shirley)\
**Post date:** [June 14, 2007, 8:31pm UTC](https://boards.straightdope.com/t/visual-basic-help-quitting-without-saving/407866/10 "2007-06-14T20:31:23Z")

</div>

[QUOTE=larsenmtl]  
Doesn’t

```auto

ActiveDocument.PrintOut Background:=False

```

do this?  
[/QUOTE]

Why, yes it does. Thank you.
