如何识别输入框中的确定和取消按钮 [英] How to identify between ok and cancel button in inputbox
问题描述
我必须写一个代码,如果用户点击在输入框中输入内容,它应该继续进行.如果它没有输入任何值,它应该再次抛出同样的问题.这我已经实现了,但是我的问题是当用户点击 CANCEl 时,它会再次询问同样的问题,而它应该退出.我对 VB 脚本很陌生.请帮助我如何处理这些按钮?下面是我现有的代码
在 x=0 时执行strAnswer = InputBox("请输入文件扩展名 * 对于所有文件:", _文件扩展名")如果 strAnswer = "" 那么MsgBox"您必须输入扩展名."别的a=字符串答案退出做万一环形整数行 = 2'strFileName = "T:\public\Madhumita\New.xls"Set objExcel = CreateObject("Excel.Application")objExcel.Visible = True设置 objWorkbook = objExcel.Workbooks.Add()'objWorkbook.SaveAs(strFileName)objExcel.Cells(1, 1).Value = "文件夹"objExcel.Cells(1, 2).Value = "文件名"objStartFolder = "T:\public\Madhumita\Madhu"Set objFSO = CreateObject("Scripting.FileSystemObject")设置 objFolder = objFSO.GetFolder(objStartFolder)设置 colFiles = objFolder.Files如果 a="*" 那么对于 colFiles 中的每个 objFileobjExcel.Cells(intRow, 1).Value = objfolder.NameobjExcel.Cells(intRow, 2).Value = objFile.NameintRow = intRow + 1下一个别的对于 colFiles 中的每个 objFilem=objFSO.GetExtensionName(objFile.Path)如果 m=a 那么objExcel.Cells(intRow, 1).Value = objfolder.NameobjExcel.Cells(intRow, 2).Value = objFile.NameintRow = intRow + 1万一下一个万一objExcel.Range("A1:B1").选择objExcel.Selection.Font.Bold = TrueobjExcel.Cells.EntireColumn.AutoFit子另存为()Application.Dialogs(xlDialogSaveAs).Show结束子objExcel.退出MsgBox "完成"
你需要处理(至少)三种情况——InputBox()返回:
- 一个空值(Empty、vbEmpty),因为用户按下了取消或关闭了对话框
- 空字符串 ("") 或空格字符串 (" ")
- 一个(希望)有效的字符串
在代码中:
选项显式做真事Dim vInp : vInp = InputBox("ee")WScript.Echo TypeName(vInp)选择大小写真Case IsEmpty(vInp)WScript.Echo中止"退出做案例"=修剪(vInp)WScript.Echo再试一次"其他情况WScript.Echo与"一起工作输入法退出做结束选择环形
示例输出:
<前>细绳再试一次空的中止细绳与 aaa 合作抱歉,文档 只是撒谎:
<块引用>如果用户单击确定或按 ENTER,则 InputBox 函数返回文本框中的任何内容.如果用户点击取消,函数返回一个长度为零的字符串 ("").
应该是:
<块引用>... 如果用户点击取消,函数返回一个空值(TypeName Empty, VarType vbEmpty).
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"
You need to deal with (at least) three cases - InputBox() returns:
- an empty value (Empty, vbEmpty) because the user pressed Cancel or closed the dialog
- an empty string ("") or a string of blanks (" ")
- 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).
这篇关于如何识别输入框中的确定和取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!