2

How can I make my VBScript cancel a process when the cancel button is pressed? When pressing "OK" I'd like the VBScript to open a new InputBox. I've gotten this to kinda work, but I'd also like to give a function the "Cancel" button. A function that whould stop the whole VBScript from opening any other MsgBox or InputBox.

I have tried something like this:

Set shell = createobject("wscript.shell")
strtext = InputBox("text")
    vbCancel
If vbCancel then 
  WScript.Quit
End if
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
AvatarLand Razer
  • 59
  • 1
  • 1
  • 9

1 Answers1

7

From MSDN:

When both helpfile and context are supplied, a Help button is automatically added to the dialog box.

If the user clicks OK or presses ENTER, the InputBox function returns whatever is in the text box. If the user clicks Cancel, the function returns a zero-length string ("").

The bold part above is not exactly true.
A user can cancel an InputBox dialog in many ways; by closing dialog, clicking Cancel, or pressing ESC, ALT + F4, and so on. In all cases except pressing or clicking OK InputBox returns Empty (an uninitalized variant, its value is 0 for numeric, zero-length string ("") for string implicitly) and it means the dialog dismissed.

Since there's an implicit conversion between a zero-length string ("") and Empty variables, looking for zero-length string equality as MSDN suggested is not always an appropriate solution(e.g. in a logic where zero-length string means "use default", not cancelled).

Dim retval
    retval = InputBox("txt")
If IsEmpty(retval) Then
    'cancelled
    MsgBox "operation has been cancelled"
Else
    'something has entered even zero-length
    MsgBox retVal
End If
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • kinda works, but after the inputbox its suposed to open a new one. – AvatarLand Razer Dec 08 '14 at 00:49
  • @AvatarLandRazer not kinda, just works. how did you implemented? you may misunderstand the concept. simply, assign the inputbox's result to a variable, then check that the variabe is empty or not. that's it. – Kul-Tigin Dec 08 '14 at 01:39
  • i acctually have gotten the whole script to work but 1 last thing now. ur solution was realy helpfull. thanks alot. but is there a way to restart the script by pressing ok aswell? – AvatarLand Razer Dec 08 '14 at 01:49
  • @AvatarLandRazer you're welcome. feel free to ask another question for the restart thing. – Kul-Tigin Dec 08 '14 at 02:02
  • Dim x x = MsgBox("finished! Whould you like to restart?",1,"title") If vbOK Then #this_is_where_i_want_a_restart_option If vbCancel then 'cancelled MsgBox("Thanks") End If is it posible to make a goto type thing? so it goes back to the start. – AvatarLand Razer Dec 08 '14 at 02:09
  • I never managed to make the IsEmpty(InputBox(...)) return true in MS Word 2016; esc, Cancel, the cross, always returns false – Jaroslav Záruba Apr 05 '20 at 08:07
  • 1
    @JaroslavZáruba This was a `vbscript` question, so this hacky solution works only for it. Microsoft Office VBA is a complete different environment. Unlike VBScript, it seems that the InputBox function in VBA works exactly as [documented](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/inputbox-function#remarks). – Kul-Tigin Apr 05 '20 at 08:30
  • 1
    @JaroslavZáruba Today I came across [this](https://stackoverflow.com/a/20909528/893670). It worked on my computer w/ Office 2016. – Kul-Tigin Apr 14 '20 at 16:02