Tips for Converting Samples to VBScript


Most of the sample code in the MSXML SDK is written in JScript or C/C++. However, much of the provided JScript code can be modified to run successfully as VBScript.

The following are some general tips for converting JScript examples to functional VBScript.

Use the CreateObject Function to Replace Instances of the ActiveXObject Object

When working with MSXML in either JScript or VBScript, you need to create an automation object (for example, a DOMDocument object), then assign its contents to a variable.

To perform this step in JScript, you create a new instance of the ActiveXObject object and assign its contents to a variable, like this:

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");

To perform this same step in VBScript, you need to use the CreateObject function, and assign the value it returns to the named variable, like this:

set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")

Note that because the value returned is of the object data type, in VBScript you need to use the set statement to assign it to the variable (xmlDoc).

Remove Line-Termination Characters Used in JScript Sample Code

In JScript, a line of code can be spread over multiple lines of text in the source file. Therefore, semi-colons are used as line termination characters to indicate the end of each line of code.

These semi-colons are not necessary in VBScript. By default, VBScript treats each line of text as a single line of code, unless you use the Visual Basic line continuation character (an underscore) to indicate that the current line wraps to the next line of text.

Declare Variables Using the Correct Syntax

In JScript, you declare a variable using the var statement, like this:

var node;

If you are declaring variables in VBScript, use the Dim statement, like this:

Dim node

Remove Parentheses as Necessary when You Set Properties or Call Methods that Require Additional Parameters

In JScript, you need to enclose parameters in parentheses when you set properties or call a method on an object. For example:


xmlDoc.setProperty("SelectionLanguage", "XPath");

currNode = xmlDoc.selectSingleNode("//book/author");

In VBScript, you need to remove the parentheses from the first two lines, like this:

xmlDoc.load "books.xml"

xmlDoc.setProperty "SelectionLanguage", "XPath"

However, you do use parentheses in VBScript when the method is used as part of the right side of an equal assignment statement. So the third line is the same in both JScript and VBScript:

currNode = xmlDoc.selectSingleNode("//book/author")

Substitute the MsgBox Function for alert() Method Calls

When you write JScript to display XML or other text string results in message boxes within Internet Explorer, you use the alert() method of the window object. To do this in VBScript, use the MsgBox function.


The following are two different HTML files that display the contents of a simple XML DOM document within an HTML page. The first file uses JScript; the second uses VBScript.


<TITLE>Displaying a Simple DOM Document in a Browser Popup</TITLE>
function displayXml() {
    var xmldoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
<BUTTON onClick="displayXml();">Display XML</BUTTON>


<TITLE>Displaying a Simple DOM Document in a Browser Popup</TITLE>
Sub DisplayXml
    set xmldoc = CreateObject("MSXML2.DOMDocument.3.0")
    xmldoc.loadXML "<root><child></child></root>"
    MsgBox xmldoc.xml
End Sub
<BUTTON onClick="DisplayXml">Display XML</BUTTON>
Try It!
  1. Open Notepad.

  2. Copy PopupUsingJScript.htm. Paste it into the Notepad window.

  3. From the File menu, click Save As. Save the file as PopupUsingJScript.htm to a folder on your computer.

  4. Copy PopupUsingVBScript.htm. Paste it into the Notepad window.

  5. From the File menu, click Save As. Save the file as PopupUsingVBScript.htm to the same folder in which you saved PopupUsingJScript.htm.

  6. Open each file in Internet Explorer, in separate browser windows. When each file appears in its own browser window, click Display XML to see output for each.


The following contents of the XML DOM document will appear as a browser popup:


The results of Step 6 are the same, regardless of whether you are executing the JScript or VBScript version of the scripted HTML page.