Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

vbscript - How to identify between ok and cancel button in inputbox

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:publicMadhumitaNew.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:publicMadhumitaMadhu"
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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...