vbscript - 如何识别输入框中的确定和取消按钮

标签 vbscript

嗨嗨我必须写一个代码,如果用户点击在输入框中输入一些东西,它应该继续。如果它没有输入任何值,它应该再次抛出同样的问题。这我已经实现了,但我的问题是当用户单击 CANCEl 时,它会再次询问相同的问题,而它应该退出。我对 VB 脚本非常陌生。请帮助我如何处理这些按钮?下面是我现有的代码

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"

最佳答案

您需要处理(至少)三种情况 - InputBox() 返回:

  • 空值 (Empty, vbEmpty) 因为用户按下了取消或关闭了对话框
  • 空字符串 ("") 或空白字符串 ("")
  • 一个(希望)有效的字符串

  • 在代码中:
    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
    

    示例输出:

    字符串
    再试一次
    空的
    中止

    字符串
    与 aaa 合作

    对不起,但是Docs只是撒谎:

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



    它应该是:

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

    关于vbscript - 如何识别输入框中的确定和取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20539295/

    相关文章:

    javascript - 使用 ASP 通过表单上传文件

    vbscript - 生成随机数字和字母

    vbscript - UFT - 随机日期

    windows - VBScript当前目录+子目录?

    javascript - 在所有窗口上方运行 hta

    batch-file - 如何将变量从 VBScript 传递到批处理?

    json - 在经典 ASP 中将 JSON 内容添加到 WinHttpRequest POST 请求时出现问题

    java - Runtime.exec() 中可以传递的参数的最大数量是多少?

    vbscript - 我们应该使用 CScript.exe 还是 WScript.exe 来运行 .vbs 文件?

    javascript - Windows Scripting Host 中的 FTP 文件?