1

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: enter image description here

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

enter image description here

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,

3
  • 1
    If you write an If-statement on more than one line, like you did, then you have to use End If. Review the MSDN page. Commented Apr 11, 2016 at 16:07
  • I tried adding EndIf, it just gave me a different error Commented Apr 11, 2016 at 16:11
  • @HansPassant could have used ` _` after the Then to avoid the need for End If. Commented Apr 11, 2016 at 21:37

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Just one question after running this, you know how I had the 1+48 in my original VBS script? Where would that go in this new version now that it is not on one line? Right now, I get the ? icon when opening, the Caution icon if clicking yes, and the critical icon if clicking "No".
It doesn't really explain why @InterLinked was getting Expected 'End' compilation error. Adding the named constants doesn't fix that but I agree they should be used, the issue was If statement spanning multiple lines without an End If or continuation character _.
1

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.

4 Comments

Wow, quick up-vote then down-vote...do the examples not work, did I not answer the question?
And how did you know if the OP downvote or other person ? and why the person who downovote don't let a message to explain why ??
Thank you for the info and you deserve from me +1 ;)
@Hackoo I apologise it seems it was someone else, now I feel terrible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.