From: Brian on
All,

The following small macro shouyld close the document if Cancel is selected,
but doesn't.

Sub AutoOpen()
Dim oDoc As Document
Set oDoc = ActiveDocument

With oDoc
Dialogs(wdDialogFileSaveAs).Show
If Response = vbCancel Then
oDoc.Close
ElseIf vbKeyEscape = True Then
Dialogs(wdDialogFileSaveAs).Close
End If
End With
End Sub


Any advive would be appreciated.

Regards,
--
Brian McCaffery
From: Jay Freedman on
Hi Brian,

Sorry to say, that code shows you have much to learn about VBA and Word.
First starters, I recommend that you read the VBA help topic named
"Displaying Built-in Word Dialog Boxes". If you can't find it in the Help,
it's online at
http://msdn.microsoft.com/en-us/library/aa157606(office.10).aspx. Also read
http://word.mvps.org/FAQs/MacrosVBA/WordDlgHelp.htm.

When the statement "Dialogs(wdDialogFileSaveAs).Show" executes, the dialog
closes itself because the user has clicked something. There is no such thing
as "Dialogs(wdDialogFileSaveAs).Close".

You used "Response", apparently expecting it to somehow contain the return
value of the dialog. But you never declared it (so VBA defaults to assuming
that it's a Variant variable with the value 0) and you never assigned any
value to it. Therefore, the value of Response can never be equal to vbCancel
(which is a built-in constant with the value 2). That's why your document
isn't closing.

If you read the help topic about the Show method, you'll find that it
returns a value with the data type of Long, and that it usually returns one
of three values: 0 if the Cancel button was pressed, -1 if the OK button was
pressed, or -2 if the Close button (if there is one in the dialog) was
pressed. You're interested only in whether or not the user clicked the OK
button. So you must assign the return value of the Show method to the
Response variable (declared as Long) and test for whether it equals -1.

Additionally, the statement "ElseIf vbKeyEscape = True Then" is complete
nonsense. vbKeyEscape is a built-in constant with the value 27, while True
is another built-in constant with the value -1. So that comparison will also
always be false.

Strictly speaking, for a tiny macro like this, assigning ActiveDocument to
the variable oDoc and using that variable once is overkill. But since it's
generally good practice (in larger macros) to use such a variable, I won't
tell you to take that out.

A working version of your macro is this:

Sub AutoOpen()
Dim Response As Long
Dim oDoc As Document
Set oDoc = ActiveDocument

Response = Dialogs(wdDialogFileSaveAs).Show
If Response <> -1 Then
oDoc.Close
End If
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Brian wrote:
> All,
>
> The following small macro shouyld close the document if Cancel is
> selected, but doesn't.
>
> Sub AutoOpen()
> Dim oDoc As Document
> Set oDoc = ActiveDocument
>
> With oDoc
> Dialogs(wdDialogFileSaveAs).Show
> If Response = vbCancel Then
> oDoc.Close
> ElseIf vbKeyEscape = True Then
> Dialogs(wdDialogFileSaveAs).Close
> End If
> End With
> End Sub
>
>
> Any advive would be appreciated.
>
> Regards,


From: Brian on
Thank you for the insight. No need to say sorry for pointing out the obvious.
:-)

Kind regards,
--
Brian McCaffery


"Jay Freedman" wrote:

> Hi Brian,
>
> Sorry to say, that code shows you have much to learn about VBA and Word.
> First starters, I recommend that you read the VBA help topic named
> "Displaying Built-in Word Dialog Boxes". If you can't find it in the Help,
> it's online at
> http://msdn.microsoft.com/en-us/library/aa157606(office.10).aspx. Also read
> http://word.mvps.org/FAQs/MacrosVBA/WordDlgHelp.htm.
>
> When the statement "Dialogs(wdDialogFileSaveAs).Show" executes, the dialog
> closes itself because the user has clicked something. There is no such thing
> as "Dialogs(wdDialogFileSaveAs).Close".
>
> You used "Response", apparently expecting it to somehow contain the return
> value of the dialog. But you never declared it (so VBA defaults to assuming
> that it's a Variant variable with the value 0) and you never assigned any
> value to it. Therefore, the value of Response can never be equal to vbCancel
> (which is a built-in constant with the value 2). That's why your document
> isn't closing.
>
> If you read the help topic about the Show method, you'll find that it
> returns a value with the data type of Long, and that it usually returns one
> of three values: 0 if the Cancel button was pressed, -1 if the OK button was
> pressed, or -2 if the Close button (if there is one in the dialog) was
> pressed. You're interested only in whether or not the user clicked the OK
> button. So you must assign the return value of the Show method to the
> Response variable (declared as Long) and test for whether it equals -1.
>
> Additionally, the statement "ElseIf vbKeyEscape = True Then" is complete
> nonsense. vbKeyEscape is a built-in constant with the value 27, while True
> is another built-in constant with the value -1. So that comparison will also
> always be false.
>
> Strictly speaking, for a tiny macro like this, assigning ActiveDocument to
> the variable oDoc and using that variable once is overkill. But since it's
> generally good practice (in larger macros) to use such a variable, I won't
> tell you to take that out.
>
> A working version of your macro is this:
>
> Sub AutoOpen()
> Dim Response As Long
> Dim oDoc As Document
> Set oDoc = ActiveDocument
>
> Response = Dialogs(wdDialogFileSaveAs).Show
> If Response <> -1 Then
> oDoc.Close
> End If
> End Sub
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
> all may benefit.
>
> Brian wrote:
> > All,
> >
> > The following small macro shouyld close the document if Cancel is
> > selected, but doesn't.
> >
> > Sub AutoOpen()
> > Dim oDoc As Document
> > Set oDoc = ActiveDocument
> >
> > With oDoc
> > Dialogs(wdDialogFileSaveAs).Show
> > If Response = vbCancel Then
> > oDoc.Close
> > ElseIf vbKeyEscape = True Then
> > Dialogs(wdDialogFileSaveAs).Close
> > End If
> > End With
> > End Sub
> >
> >
> > Any advive would be appreciated.
> >
> > Regards,
>
>
> .
>