Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify between ok and cancel button in inputbox

Tags:

vbscript

Hi Hi I have to write a code where if the user clicks enters something in the input box it should proceed further.If it doesnot enter any value it should throw back the same question again again.This i have already achieved,but my problem is when user click on CANCEl it agains asks the same question whereas it ishould exit .I am very new to VB Script .Plz help me how to handle these buttons?Below is my existing code

Do while x=0
strAnswer = InputBox("Please enter the file extension  *  For all files:", _
    "File Extension")
If strAnswer = "" Then
        MsgBox"You must enter an extension."

Else

        a=strAnswer
        Exit Do
    End If
Loop


intRow = 2
'strFileName = "T:\public\Madhumita\New.xls"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
'objWorkbook.SaveAs(strFileName)
objExcel.Cells(1, 1).Value = "Folder"
objExcel.Cells(1, 2).Value = "File Name"
objStartFolder = "T:\public\Madhumita\Madhu"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
If a="*" Then
For Each objFile in colFiles
objExcel.Cells(intRow, 1).Value = objfolder.Name
objExcel.Cells(intRow, 2).Value = objFile.Name
intRow = intRow + 1
Next

else

For Each objFile in colFiles
m=objFSO.GetExtensionName( objFile.Path )

If m=a Then

objExcel.Cells(intRow, 1).Value = objfolder.Name
objExcel.Cells(intRow, 2).Value = objFile.Name
intRow = intRow + 1


End If
Next
End If 
objExcel.Range("A1:B1").Select
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit
Sub SaveAs() 
    Application.Dialogs(xlDialogSaveAs).Show 
End Sub 


objExcel.Quit
MsgBox "Done"
like image 824
user3086267 Avatar asked Feb 14 '23 09:02

user3086267


1 Answers

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

like image 163
Ekkehard.Horner Avatar answered Feb 23 '23 20:02

Ekkehard.Horner