Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office

Tips for Converting Samples to VBScript

Note   This topic is intended to provide some guidance for VBScript developers. For more information about JScript or VBScript, see the documentation available from the MSDN Scripting Developer's Center .

Most of the sample code in the MSXML SDK is written in JScript, Visual Basic, 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.5.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.5.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 needed 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.load("books.xml");
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.

Example

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. The differences are highlighted in bold text.

PopupUsingJScript.htm

<HTML>
<HEAD>
<TITLE>Displaying a Simple DOM Document in a Browser Popup</TITLE>
<SCRIPT LANGUAGE="JScript">
function displayXml() {
    var xmldoc = new ActiveXObject("MSXML2.DOMDocument.5.0");
    xmldoc.loadXML("<root><child></child></root>");
    alert(xmldoc.xml);
}
</SCRIPT>
</HEAD>
<BODY>
<BUTTON onClick="displayXml();">Display XML</BUTTON>
</BODY>
</HTML>

PopupUsingVBScript.htm

<HTML>
<HEAD>
<TITLE>Displaying a Simple DOM Document in a Browser Popup</TITLE>
<SCRIPT LANGUAGE="VBScript">
Sub DisplayXml
    set xmldoc = CreateObject("MSXML2.DOMDocument.5.0")
    xmldoc.loadXML "<root><child></child></root>"
    MsgBox xmldoc.xml
End Sub
</SCRIPT>
</HEAD>
<BODY>
<BUTTON onClick="DisplayXml">Display XML</BUTTON>
</BODY>
</HTML>

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.

Output

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

<root><child></child></root>

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