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

十年热恋 提交于 2019-12-08 08:59:47

问题


I have been trying to get a VBS script to work for a while now with msgbox. When I use a single msgbox statement, it works. 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"

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. 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.

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,

I would appreciate any assistance, even if it is small,


回答1:


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




回答2:


While @Hackoo's answer is technically correct it doesn't answer the initial question, so I'll attempt to here.

The reason for the error

Microsoft VBScript compilation error: Expected 'End'

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 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

    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.

    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.



来源:https://stackoverflow.com/questions/36553287/how-do-i-properly-configure-conditional-actions-with-user-input-in-vbs-with-msgb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!