如何制作带有两个执行不同操作选项的VBScript MsgBox?
VBScript 多选项MsgBox代码修复
原代码的核心问题
- If语句未与返回值变量做比较,直接写
vbYes then属于语法错误 - 缺少
End If闭合语句,代码结构不完整 Wscript CreateObject写法错误,正确语法是WScript.CreateObject- 消息框图标常量混用(
vbQuestion + vbInformation不合法,同一消息框只能用一种图标) - 逻辑分支混乱,嵌套If未正确缩进和闭合
Return不是VBScript合法语句,实现返回主消息框需用循环do Nothing写法不符合VBScript语法规范
修正后的代码
Dim userChoice Do ' 循环实现返回主消息框的功能 userChoice = MsgBox("Do you want to open the Learning Program?", vbYesNoCancel + vbQuestion, "Activate the Program") Select Case userChoice Case vbYes ' 二次确认是否打开程序 Dim confirmOpen confirmOpen = MsgBox("Are you Sure?", vbYesNo + vbQuestion, "Confirm Opening") If confirmOpen = vbYes Then ' 创建Shell对象并启动目标程序(替换为你的学习程序路径) Dim shell Set shell = WScript.CreateObject("WScript.Shell") shell.Run "notepad.exe" ' 此处替换为实际程序的完整路径 Set shell = Nothing ' 释放对象资源 Exit Do ' 完成操作后退出循环 End If ' 选No则回到主消息框 Case vbNo, vbCancel ' 二次确认是否退出 Dim confirmExit confirmExit = MsgBox("Are you Sure to leave?", vbYesNo + vbQuestion, "Exit Learning Program") If confirmExit = vbYes Then Exit Do ' 确认退出则结束脚本 End If ' 选No则回到主消息框 End Select Loop
代码说明
- 用
Do...Loop循环实现用户取消/不确认时返回主消息框的需求 - 用
Select Case替代冗余的If...ElseIf,逻辑结构更清晰 - 修正消息框常量使用,同一消息框仅保留一种图标类型(
vbQuestion) - 正确创建
WScript.Shell对象,通过Run方法启动目标程序(示例为记事本,需替换为你的学习程序路径) - 每个分支都有明确的语法闭合,符合VBScript规范
内容的提问来源于stack exchange,提问作者Fabian
相关产品推荐
相关产品推荐

