简体   繁体   English

如何使用MsgBox在VBS中使用用户输入正确配置条件操作?

[英]How do I properly configure conditional actions with user input in VBS with MsgBox?

I have been trying to get a VBS script to work for a while now with msgbox. 我一直在尝试使用msgbox让VBS脚本工作一段时间。 When I use a single msgbox statement, it works. 当我使用单个msgbox语句时,它可以工作。 As soon as I start adding conditional input options, then it doesn't work. 一旦我开始添加条件输入选项,它就不起作用。

I posted this question on Super User and I was told to use the "dim" statement, and to post on this website, and I have done both now. 我在超级用户上发布了这个问题,我被告知要使用“昏暗”的声明,并在这个网站上发帖,我现在已经完成了。 Here is some of the code I am trying that works. 以下是我正在尝试的一些代码。 (Please ignore my example.) (请忽略我的例子。)

Option Explicit
Dim vbsmsg, vbsyes, vbsno
vbsmsg=MsgBox("Proceeding will wipe the contents of your C: Drive. Proceed?", 1+48, "Format Drive C:")

When I run the above code via a shortcut I get a dialog like this: 当我通过快捷方式运行上面的代码时,我得到一个这样的对话框: 在此输入图像描述

But if I add the following, I get a run-time error when clicking "OK" or "Cancel" 但是,如果我添加以下内容,单击“确定”或“取消”时会出现运行时错误

If vbsmsg=1 Then
    vbsyes=MsgBox("The contents of your C: Drive could not be successfully deleted.", 0+64, "Error Formatting Drive C: - System Error 5")
If vbsmsg=2 Then
    vbsno=MsgBox("Not all of the contents of your C: Drive were successfully deleted. Please try again.", 0+64, "Error Formatting Drive C: - System Error 303")

在此输入图像描述

The line/character in the error is between the "0" and "3" in "System Error 303" 错误中的行/字符位于“系统错误303”中的“0”和“3”之间

I have tried a great deal of troubleshooting already. 我已经尝试了很多故障排除。 I have tried altering the dim statement, adding option explicit, using 1 and 2 instead of 6 and 8, etc... nothing seems to work. 我已经尝试改变昏暗的声明,添加选项显式,使用1和2而不是6和8等...似乎没有任何工作。 When I commented out the 2nd part, instead of getting an error after executing the file, it just closed on me. 当我注释掉第二部分时,不是在执行文件后出错,而是关闭了我。 I am positive all of my syntax is correct and in the right format. 我很肯定我的所有语法都是正确的并且格式正确。 I changed 1 and 2 to vbOK and vbCancel and when I changed it back it wouldn't work at all and gave me the error pictured on this page right away. 我将1和2更改为vbOK和vbCancel,当我将其更改回来时它根本不起作用并立即给出了我在此页面上所描绘的错误。

If anyone knows what is wrong with my examples, I would greatly appreciate it. 如果有人知道我的例子有什么问题,我会非常感激。 I am fairly new to working with VBS files, but I have been working with .bat files for a long time and none of those principles seem to be working here, 我对使用VBS文件相当陌生,但我一直在使用.bat文件很长一段时间,这些原则似乎都没有在这里工作,

I would appreciate any assistance, even if it is small, 我会感激任何帮助,即使它很小,

Give this example a try: 尝试这个例子:

Option Explicit
Dim Title,Question
Title = "user input in VBS with MsgBox"
Question = MsgBox("Proceeding will wipe the contents of your C: Drive. Proceed ?",vbYesNo+vbQuestion, Title)
If Question = vbYes Then
    MsgBox "We proceed wipping your C:\ drive",vbExclamation,Title
    'Call your sub here to continue proceeding your script
Else
    MsgBox "Canceling the operation !",vbCritical,Title
    Wscript.Quit()
End If

For more information about MsgBox Constants 有关MsgBox常量的更多信息

While @Hackoo's answer is technically correct it doesn't answer the initial question , so I'll attempt to here. 虽然@ Hackoo的 答案在技​​术上是正确的,但它没有回答最初的问题 ,所以我会尝试在这里。

The reason for the error 出错的原因

Microsoft VBScript compilation error: Expected 'End' Microsoft VBScript编译错误:预期'结束'

is due to the If statement spanning more then one line without an End If to finish the statement block, as in @Hackoo's example adding End If will correct this error. 是由于If语句跨越多行而没有End If要完成语句块,就像在@ Hackoo的示例中添加End If将更正此错误。

If for whatever reason you wanted to keep the syntax condensed you weren't far away you had two options; 如果由于某种原因你想保持语法浓缩,你不远处有两个选择;

  1. Put the If statements all on one line If语句全部放在一行上

     Option Explicit Dim vbsmsg vbsmsg = MsgBox("Proceeding will wipe the contents of your C: Drive. Proceed?", vbYesNo + vbQuestion, "Format Drive C:") If vbsmsg = vbYes Then Call MsgBox("The contents of your C: Drive could not be successfully deleted.", vbExclamation, "Error Formatting Drive C: - System Error 5") If vbsmsg = vbNo Then Call MsgBox("Not all of the contents of your C: Drive were successfully deleted. Please try again.", vbCritical, "Error Formatting Drive C: - System Error 303") 

    which can be a little ugly looking at sometimes hard to follow (but that's just my opinion) . 这有点难看,有时难以遵循(但这只是我的意见)

  2. Use the Line Continuation Character ( _ ) to allow a single statement to span multiple lines, in VBScript this is also known as a Statement Break . 使用行连续字符_允许单个语句跨越多行,在VBScript中,这也称为语句中断

     Option Explicit Dim vbsmsg vbsmsg = MsgBox("Proceeding will wipe the contents of your C: Drive. Proceed?", vbYesNo + vbQuestion, "Format Drive C:") If vbsmsg = vbYes Then _ Call MsgBox("The contents of your C: Drive could not be successfully deleted.", vbExclamation, "Error Formatting Drive C: - System Error 5") If vbsmsg = vbNo Then _ Call MsgBox("Not all of the contents of your C: Drive were successfully deleted. Please try again.", vbCritical, "Error Formatting Drive C: - System Error 303") 

As already mentioned it goes without saying that you should endeavour to use the VBScript Named Constants in code wherever possible instead of hard coded numeric values. 如前所述,不言而喻,您应该尽可能在代码中使用VBScript命名常量而不是硬编码数值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM