嗨嗨我必须编写一个代码,如果用户点击进入输入框中的内容,它应该继续进行。如果它没有输入任何值,它应该再次抛出相同的问题。这我已经实现了,但是我的问题是,当用户点击CANCEl时,它会再次询问相同的问题,而它应该退出。我是VB Script的新手.Plz帮助我如何处理这些按钮?下面是我现有的代码
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"
答案 0 :(得分:9)
你需要处理(至少)三种情况 - InputBox()返回:
在代码中:
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
示例输出:
String Try again Empty Abort String Work with aaa
很抱歉,但是Docs只是撒谎:
如果用户单击“确定”或按ENTER键,则返回InputBox函数 文本框中的内容。如果用户单击“取消”,则为该功能 返回零长度字符串(“”)。
应该是:
...如果用户单击“取消”,则该函数返回空值 (TypeName Empty,VarType vbEmpty)。
答案 1 :(得分:0)
对于InputBox()
,您可以使用默认值确定用户是单击“取消”还是单击“确定”或按Enter继续输入而不输入值:
Sub Get_TIN()
TIN = Trim(InputBox("Enter the provider TIN:", "Provider TIN", "ex. 123456789"))
If TIN = "" Then 'When CANCEL is clicked because "TIN" will be empty.
MsgBox "You pressed Cancel. Program will now end.", vbExclamation + vbOKOnly, "Macro End"
Exit Sub
End If
If IsEmpty(TIN) = False Then 'When OK is clicked or Enter pressed because default text will be stored. Next, set TIN to "".
TIN = ""
End If
End Sub
答案 2 :(得分:-1)
我正在使用VBS,我对cancel / ok的调查显示以下内容:
取消返回一个空字符串和一个零长度字符串 - 你说的相同吗?显然不是。
Ok只返回零长度字符串。
我使用下面的代码来区分。
if IsEmpty(nmbr) then 'cancel button pressed ?
nmbr = "x"
end if
if not IsEmpty(nmbr) then 'ok button pressed ?
if len(nmbr) = 0 then
nmbr = "ok"
end if
end if