How to cancel an input box

S

singeredel

Below is the code I have that checks to see if the file name already exists
and, if so, provides an input box to put in a different file name before
saving. The problem I have with this is the Cancel button on the input box
just erases the string text that was already placed in the input box (the
file name being duplicated). I would like to know how to actually cancel out
of the input box and stop the code from executing when I just want to quit
the whole process.

If there is a better way of accomplishing the checking for a duplicate file
name and being able to replace it with a modified file name (or exiting the
whole process altogether), that would be appreciated too.

Thanks...


SaveFile:
Dim TestFile As String
TestFile = Dir(SaveCurrentFile)
If Len(TestFile) = 0 Then
ActiveDocument.SaveAs (SaveCurrentFile), FileFormat:= _
wdFormatDocument
'ChDir DictationDateFolder
ChangeFileOpenDirectory (DictationDateFolder)
GoTo MakeHeader2
Else:
FileExists:
Dim ResponseFileExists As String 'VbMsgBoxResult
ResponseFileExists = InputBox("The file name " + SaveCurrentFile +
Chr(13) + _
"already exists in the current directory" + Chr(13) + Chr(13) + _
"Type new file name below:", "FileExists", SaveCurrentFile)
SaveCurrentFile = ResponseFileExists
GoTo SaveFile
End If
 
G

Greg Maxey

Julie,

The code you provided is incomplete. What is "SaveCurrentFile"

You can use StrPtr to determine if a user presses OK or Cancel on an
Inputbox dialog:

Sub ScratchMacro()
Dim UserText As String
UserText = InputBox("Type something:")
If StrPtr(UserText) = 0 Then
Debug.Print "User pressed Cancel"
Exit Sub
Else
Debug.Print "User pressed OK"
'Do something
End If
End Sub
 
S

singeredel

Yes, this is not the entire programming code, just the part I needed help
with. SaveCurrentFile is just a variable that holds the file name.
 
G

Greg Maxey

Its hard to see if there is a better way if the way you provide isn't
complete ;-)
 
S

singeredel

Since hitting the cancel button just erases the string, would I then have to
hit the OK button to activate the exit sub?

-- singeredel (julie)
 
S

singeredel

Unfortunately I don't think you would want to have to wade through the amount
of code involved in seeing the whole program.

Thanks for your help!
 

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