Quoting Quotes

Published on July 24, 2008 by Marcus Tettmar in Scripting

VBScript and Complex Expressions use the ” (double quote) character to delimit strings. Native MacroScript code doesn’t need delimited strings. But if you need to use Complex Expressions or VBScript you need to remember that strings must be delimited in quotes.

One implication of this is that if the string already has a ” character within it you will get a syntax error, because the parser thinks this marks the end of the string.

To avoid this you need to add another quote, and you can use StringReplace to replace any occurences of the ” character with a second one before passing the string to VBScript or a Complex Expression:

//example input string:
Let>mystring=your name is "Sally"

//Double quote before using string in complex expression
StringReplace>mystring,","",mystring_2
If>{%mystring_2% <> ""}
  //Do something
Endif

Without that StringReplace call to double quote the quote characters, the complex If line would produce a syntax error.

The same goes for VBScript:

//example input string:
Let>mystring=your name is "Sally"

VBSTART
VBEND

StringReplace>mystring,","",mystring_2
VBEval>MsgBox("%mystring_2%"),nul

Note that the VBEval statement evaluates a pure VBScript expression. Since VBScript strings must be in quotes we surround the variable mystring_2 with them, and need to use % symbols to tell Macro Scheduler to use the value of mystring_2. See my last post for tips on when and where to use % symbols.

On a similar note, CR LF pairs (Carriage Return and Line Feed) in strings passed to VBScript will produce errors since they are hard line breaks and cause string termination problems. We can avoid this by replacing the real CR and LF characters with the VBScript placeholders:

StringReplace>myString,CRLF," & vbCRLF & ",myString

The quotes terminate and start the string again, splitting it where the CRLF was, and replacing the hard CRLF with the VBScript vbCRLF variable. There’s also vbCR and vbLF variables for carriage returns (CR) and line feeds (LF) on their own.