About using Double quotes in Vbscript

In VBScript, string literals are surrounded by double quotes ("). This is what your first example shows:

Msgbox "This is myName" ' This works fine

However, if you want to include a double quote character inside of your string literal, you’ve got a problem, because VBScript is going to interpret the second double quote character it finds as signifying the end of the string literal. This is what your second example shows:

Msgbox "This is "myName""  ' This gives an error
                       ^   ' because it prematurely terminates the string here
                           ' and doesn't know what to do with the trailing "

Fortunately, there’s an escape hatch. It involves escaping the double quote character with another character, indicating that VBScript should process it as a literal double quote character, rather than a magical “end-of-string-literal” character. It just so happens that the escape character VBScript uses is a double quote character. This is what your second example shows:

Msgbox "This is ""myName"""   'This works fine
  • You begin the string with a single double-quote, indicating the start of a string literal.
  • Then you want to have an embedded double quote character, so you use two of them. This is where the escaping starts: you escape the double quote character with another double quote character.
  • Then you do that escaping thing again.
  • Finally, you terminate the entire string literal with another double quote character.

Other languages often use a backslash (\) as the escape character. That might make things easier to see. Assuming VBScript used a backslash as the escape character rather than a double quote, your code would look like this:

Msgbox "This is \"myName\""   ' doesn't work in VBScript; example only

If this syntax bothers you, you can declare a constant for the double quote and use that each time:

Const Quote = """"

' ... later in the code ...

Msgbox "This is " & Quote & "myName" & Quote

Leave a Comment