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!

Re: How to handle pressing button "Cancel" from InputBox? by Jay

Jay
Tue Jul 15 07:56:32 PDT 2008

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

avkokin wrote:
> 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!



Re: How to handle pressing button "Cancel" from InputBox? by avkokin

avkokin
Tue Jul 15 10:24:57 PDT 2008

On Jul 15, 6:56=A0pm, "Jay Freedman" <jay.freed...@verizon.net> wrote:
> 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, b=
ut
> 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 y=
ou
> 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 =A0 =A0 =A0 =A0FAQ:http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup=
so
> all may benefit.
>
>
>
> avkokin wrote:
> > 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 =3D wdSelectionInlineShape Then
> > =A0 Selection.InlineShapes(1).ConvertToShape
> > End If
> > If Selection.Type =3D wdSelectionIP Then
> > =A0 MsgBox "Please select your image"
> > Else
> > =A0 Do While Message =3D ""
> > =A0 =A0 =A0Message =3D InputBox("Please enter the number." & _
> > =A0 =A0 =A0vbCr & "...", _
> > =A0 =A0 =A0"Move image", "")
> > =A0 =A0 =A0 =A0 If Message =3D "" Then
> > =A0 =A0 =A0 =A0 =A0 =A0MsgBox "Please enter the number"
> > =A0 =A0 =A0 =A0 End If
> > =A0 Loop
> > =A0 Selection.ShapeRange.IncrementLeft MillimetersToPoints(Message)
> > End If
> > End Sub
>
> > Thank you very much!- Hide quoted text -
>
> - Show quoted text -

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

Thank you.

Re: How to handle pressing button "Cancel" from InputBox? by Karl

Karl
Tue Jul 15 13:46:23 PDT 2008

Jay Freedman wrote:
> 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

--
.NET: It's About Trust!
http://vfred.mvps.org



Re: How to handle pressing button "Cancel" from InputBox? by Karl

Karl
Tue Jul 15 14:02:18 PDT 2008

Jay Freedman wrote:
> Karl E. Peterson wrote:
>> Jay Freedman wrote:
>>> 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
>
> Thanks, Karl. That's my "you learn something every day" for today. :-)

Hey cool! Time to go home, then! :-)
--
.NET: It's About Trust!
http://vfred.mvps.org



Re: How to handle pressing button "Cancel" from InputBox? by Jay

Jay
Tue Jul 15 13:59:02 PDT 2008


Karl E. Peterson wrote:
> Jay Freedman wrote:
>> 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

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

--
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.



Re: How to handle pressing button "Cancel" from InputBox? by Gregory

Gregory
Tue Jul 15 14:16:31 PDT 2008

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


"Karl E. Peterson" <karl@mvps.org> wrote in message
news:eBA7Yvr5IHA.2348@TK2MSFTNGP06.phx.gbl...
> Jay Freedman wrote:
>> 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
>
> --
> .NET: It's About Trust!
> http://vfred.mvps.org
>



Re: How to handle pressing button "Cancel" from InputBox? by Karl

Karl
Tue Jul 15 14:39:27 PDT 2008

Gregory K. Maxey wrote:
> 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.
--
.NET: It's About Trust!
http://vfred.mvps.org



Re: How to handle pressing button "Cancel" from InputBox? by Gregory

Gregory
Tue Jul 15 14:51:47 PDT 2008

Karl,

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

Thanks.

Karl E. Peterson wrote:
> Gregory K. Maxey wrote:
>> 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.



Re: How to handle pressing button "Cancel" from InputBox? by avkokin

avkokin
Tue Jul 15 23:13:06 PDT 2008

On Jul 15, 9:24=A0pm, avkokin <avko...@gmail.com> wrote:
> On Jul 15, 6:56=A0pm, "Jay Freedman" <jay.freed...@verizon.net> wrote:
>
>
>
>
>
> > Quoting the VBA Help topic for the InputBox function, 'If the user clic=
ks
> > 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 InputBo=
x
> > 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 comma=
nd or
> > GoTo a label at the end of the procedure).
>
> > The other alternative is to replace the InputBox with a UserForm that y=
ou
> > 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 mac=
ro
> > can check that variable's value before proceeding to use the string fro=
m the
> > text box.
>
> > --
> > Regards,
> > Jay Freedman
> > Microsoft Word MVP =A0 =A0 =A0 =A0FAQ:http://word.mvps.org
> > Email cannot be acknowledged; please post all follow-ups to the newsgro=
up so
> > all may benefit.
>
> > avkokin wrote:
> > > 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 =3D wdSelectionInlineShape Then
> > > =A0 Selection.InlineShapes(1).ConvertToShape
> > > End If
> > > If Selection.Type =3D wdSelectionIP Then
> > > =A0 MsgBox "Please select your image"
> > > Else
> > > =A0 Do While Message =3D ""
> > > =A0 =A0 =A0Message =3D InputBox("Please enter the number." & _
> > > =A0 =A0 =A0vbCr & "...", _
> > > =A0 =A0 =A0"Move image", "")
> > > =A0 =A0 =A0 =A0 If Message =3D "" Then
> > > =A0 =A0 =A0 =A0 =A0 =A0MsgBox "Please enter the number"
> > > =A0 =A0 =A0 =A0 End If
> > > =A0 Loop
> > > =A0 Selection.ShapeRange.IncrementLeft MillimetersToPoints(Message)
> > > End If
> > > End Sub
>
> > > Thank you very much!- Hide quoted text -
>
> > - Show quoted text -
>
> Hello.
> I doing that:
> Sub movePic()
> Dim Message As String
> On Error Resume Next
> If Selection.Type =3D wdSelectionIP Then
> =A0 =A0MsgBox "Please select your image"
> Else
> =A0 =A0Do While Message =3D ""
> =A0 =A0 =A0 Message =3D InputBox("Please enter the number." & vbCr & "...=
",
> "Move image", "")
> =A0 =A0 =A0 If Message =3D vbNullString Then Exit Sub
> =A0 =A0 =A0 If Message =3D "" Then
> =A0 =A0 =A0 =A0 =A0MsgBox "Please select your image"
> =A0 =A0 =A0 End If
> =A0 =A0Loop
> =A0 =A0If Selection.Type =3D wdSelectionInlineShape Then
> =A0 =A0 =A0 Selection.InlineShapes(1).ConvertToShape
> =A0 =A0End If
> =A0 =A0Selection.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 =3D "" Then
MsgBox "Please select your image"
End If