gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:22:20 25 4
gpt4 key购买 nike

嗨嗨我必须写一个代码,如果用户点击在输入框中输入一些东西,它应该继续。如果它没有输入任何值,它应该再次抛出同样的问题。这我已经实现了,但我的问题是当用户单击 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com