How to handle pressing button "Cancel" from InputBox?

A

avkokin

Hello.
How to handle of pressing button "Cancel" from dialogue InputBox? In
my code when user press button "Cancel" dispaly Msgbox from cycle Do
While. I need to cancel all action. See my code:
Sub movePic()
Dim Message As String
On Error Resume Next
If Selection.Type = wdSelectionInlineShape Then
Selection.InlineShapes(1).ConvertToShape
End If
If Selection.Type = wdSelectionIP Then
MsgBox "Please select your image"
Else
Do While Message = ""
Message = InputBox("Please enter the number." & _
vbCr & "...", _
"Move image", "")
If Message = "" Then
MsgBox "Please enter the number"
End If
Loop
Selection.ShapeRange.IncrementLeft MillimetersToPoints(Message)
End If
End Sub

Thank you very much!
 
J

Jay Freedman

Quoting the VBA Help topic for the InputBox function, 'If the user clicks
Cancel, the function returns a zero-length string ("").' There is no
separate indication that the user clicked the Cancel button instead of
clicking Enter when the text box is empty -- as far as the macro is
concerned, those two actions are exactly the same. This is unfortunate, but
it has always been that way.

You have two alternatives. One is to accept the behavior of the InputBox
function; remove the Do While loop and assume that a returned value of an
empty string just means "cancel the whole macro" (use an Exit Sub command or
GoTo a label at the end of the procedure).

The other alternative is to replace the InputBox with a UserForm that you
create, containing a text box, a label, and OK and Cancel buttons. Then you
can define a variable in the General Declarations part of the UserForm's
code to indicate whether the Cancel button was clicked. The calling macro
can check that variable's value before proceeding to use the string from the
text box.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
A

avkokin

Quoting the VBA Help topic for the InputBox function, 'If the user clicks
Cancel, the function returns a zero-length string ("").' There is no
separate indication that the user clicked the Cancel button instead of
clicking Enter when the text box is empty -- as far as the macro is
concerned, those two actions are exactly the same. This is unfortunate, but
it has always been that way.

You have two alternatives. One is to accept the behavior of the InputBox
function; remove the Do While loop and assume that a returned value of an
empty string just means "cancel the whole macro" (use an Exit Sub commandor
GoTo a label at the end of the procedure).

The other alternative is to replace the InputBox with a UserForm that you
create, containing a text box, a label, and OK and Cancel buttons. Then you
can define a variable in the General Declarations part of the UserForm's
code to indicate whether the Cancel button was clicked. The calling macro
can check that variable's value before proceeding to use the string from the
text box.

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





- Show quoted text -

Hello.
I doing that:
Sub movePic()
Dim Message As String
On Error Resume Next
If Selection.Type = wdSelectionIP Then
MsgBox "Please select your image"
Else
Do While Message = ""
Message = InputBox("Please enter the number." & vbCr & "...",
"Move image", "")
If Message = vbNullString Then Exit Sub
If Message = "" Then
MsgBox "Please select your image"
End If
Loop
If Selection.Type = wdSelectionInlineShape Then
Selection.InlineShapes(1).ConvertToShape
End If
Selection.ShapeRange.IncrementLeft MillimetersToPoints(Message)
End If
End Sub

Thank you.
 
K

Karl E. Peterson

Jay said:
Quoting the VBA Help topic for the InputBox function, 'If the user clicks
Cancel, the function returns a zero-length string ("").' There is no
separate indication that the user clicked the Cancel button instead of
clicking Enter when the text box is empty -- as far as the macro is
concerned, those two actions are exactly the same. This is unfortunate, but
it has always been that way.

Actually, that's the way it's documented, but that isn't totally true...

Private Sub Command1_Click()
Dim UserText As String
UserText = InputBox("Type something:")
If StrPtr(UserText) = 0 Then
Debug.Print "User pressed Cancel"
Else
Debug.Print "User pressed OK"
End If
End Sub
 
J

Jay Freedman

Karl said:
Actually, that's the way it's documented, but that isn't totally
true...
Private Sub Command1_Click()
Dim UserText As String
UserText = InputBox("Type something:")
If StrPtr(UserText) = 0 Then
Debug.Print "User pressed Cancel"
Else
Debug.Print "User pressed OK"
End If
End Sub

Thanks, Karl. That's my "you learn something every day" for today. :)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Gregory K. Maxey

Karl,

I don't know where I got it because StrPtr has no associated text, but I had
the follow snippet of code tucked away in my toolbox:

Sub ScratchMacro()
Dim pResult As String
Do
pResult = InputBox("Type your name if the text field:", "Client Name")
pResult = RealInput(pResult)
If pResult = "Input canceled by user!" Then
Exit Do
End If
Loop Until pResult <> ""
End Sub

Function RealInput(pInput As String) As String
If StrPtr(pInput) = 0 Then
MsgBox "Input Canceled by User!"
RealInput = "Input canceled by user!"
Else
If pInput = "" Then
RealInput = ""
MsgBox "You did not provide an input"
Else
RealInput = pInput
End If
End If
End Function
 
K

Karl E. Peterson

Gregory said:
I don't know where I got it because StrPtr has no associated text,

StrPtr simply returns the memory address (within your process's virtual memory
range) of the BSTR data. It's indispensible when working with APIs, in particular
the Unicode variations. Matt Curland wrote up an "unofficial" document on them
(*Ptr functions) some time ago...

http://vb.mvps.org/tips/varptr.asp

....and was kind enough to ask me to post it on my site.
 
G

Gregory K. Maxey

Karl,

Yes. I just read that (again) and I am pretty sure that is where I came upon
StrPtr the first time.

Thanks.
 
A

avkokin

Hello.
I doing that:
Sub movePic()
Dim Message As String
On Error Resume Next
If Selection.Type = wdSelectionIP Then
   MsgBox "Please select your image"
Else
   Do While Message = ""
      Message = InputBox("Please enter the number." & vbCr & "...",
"Move image", "")
      If Message = vbNullString Then Exit Sub
      If Message = "" Then
         MsgBox "Please select your image"
      End If
   Loop
   If Selection.Type = wdSelectionInlineShape Then
      Selection.InlineShapes(1).ConvertToShape
   End If
   Selection.ShapeRange.IncrementLeft MillimetersToPoints(Message)
End If
End Sub

Thank you.- Hide quoted text -

- Show quoted text -

I found that next code in my macro is superfluous:
If Message = "" Then
MsgBox "Please select your image"
End If
 

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