How to identify between ok and cancel button in inputbox

You need to deal with (at least) three cases – InputBox() returns:

  1. an empty value (Empty, vbEmpty) because the user pressed Cancel or closed the dialog
  2. an empty string (“”) or a string of blanks (” “)
  3. a (hopefully) valid string

In code:

Option Explicit

Do While True
   Dim vInp : vInp = InputBox("ee")
   WScript.Echo TypeName(vInp)
   Select Case True
     Case IsEmpty(vInp)
       WScript.Echo "Abort"
       Exit Do
     Case "" = Trim(vInp)
       WScript.Echo "Try again"
     Case Else
       WScript.Echo "Work with " & vInp
       Exit Do
   End Select
Loop

sample output:

String
Try again
Empty
Abort

String
Work with aaa

Sorry to say, but the Docs just lie:

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 (“”).

It should be:

… If the user clicks Cancel, the function returns an empty value
(TypeName Empty, VarType vbEmpty).

Leave a Comment