Message Box Cancel Button

R

Rita King

Hi - Using Word 2002. I have a dialog box where the user inputs the name of
the file they wish to have open. The box has the OK and Cancel buttons. If
I run the macro and don't type in any file name but simply press Cancel I
get an error (run time error 5273 - the doc name or path is not valid). How
do I capture when the user presses Cancel and get the macro to simply quit?

Many thanks
 
D

Doug Robbins - Word MVP

Hi Rita,

With an InputBox, if the user presses cancel, it will return a zero length
string "". There for, you should use an If..Then..Else ..End If
construction to check the response and exit the sub if the result is ""

However, you could use:

Dialogs(wdDialogFileOpen).Show

to display the File Open dialog so that the user can select the file.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
R

Rita King

Thanks for the reply Doug.

I am lost with this still (I am a complete beginner) - here's what I have

Sub proInputText()
Dim strAns As String
strAns = InputBox("Type the number of the" & Chr(13) & _
"paragraph you wish to insert", "Will Retrieval")

ChangeFileOpenDirectory "Y:\Masters\WILLS\"
Selection.InsertFile FileName:=strAns, Range:="", ConfirmConversions:= _
True, Link:=False, Attachment:=False

End Sub

So to capture the fact that the user may change her mind and press cancel, I
need to put:

If strAns = "" Then
***what?***
Else ChangeFileOpenDirectory "Y: etc.
End If
 
D

Doug Robbins - Word MVP

Hi Rita,

Use

Sub proInputText()
Dim strAns As String
strAns = InputBox("Type the number of the" & Chr(13) & _
"paragraph you wish to insert", "Will Retrieval")
If strAns = "" Then
Exit Sub
Else
ChangeFileOpenDirectory "Y:\Masters\WILLS\"
Selection.InsertFile FileName:=strAns, Range:="", ConfirmConversions:= _
True, Link:=False, Attachment:=False
End If
End Sub


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top