The COM technology behind objects such as the Microsoft Scripting Runtime Library's FileSystemObject object (aka the FSO object) and Windows Script Host's (WSH's) WshShell object is what makes WSH scripts so powerful. However, the way this technology works causes scripters grief in one area: Scripts don't get access to the crucial constants embedded in objects.
If you use the FSO object's Open-TextFile method to open a file for writing or appending, you might have run into this problem. In its simplest form, this method takes one mandatory parameter—the name of a file—and opens that file so you can read it. For example, the code
Dim fso
Set fso = CreateObject( _
"Scripting.FileSystemObject")
Set file = fso.OpenTextFile( _
"C:\temp\computers.txt")
opens C:\temp\computers.txt for reading. The OpenTextFile method has three optional parameters, one of which is the I/O mode. When you look at the documentation for using this parameter, you'll notice that it can take one of three constants: ForReading, ForWriting, or ForAppending. However, if you try to use ForAppending to open and add data to the file with code such as
Set file = fso.OpenTextFile( _
"C:\temp\computers.txt", _
ForAppending, True)
VBScript will immediately give you the runtime error Invalid procedure call or argument. If you're a cautious scripter who uses Option Explicit to make sure all variables are defined, you get an even more puzzling but more accurate error message: Variable is undefined: 'ForAppending'. The problem is that you're using ForAppending as a constant, but the VBScript engine sees it as a variable. You'll receive similar error messages when you use ForReading and ForWriting.
Why do scripters have this problem when programmers who use compilers don't? To answer that question, you need to understand a little about type libraries. Microsoft defines a type library as, "A file or component within another file that contains type information about exposed objects." The FSO object is a type. Every object you create in a script is an instance of a type, which you load from a type library. Constants are special types called enumerations, which are also in type libraries.
When you use VBScript's CreateObject function with the FSO object, you're dealing directly with a type within a specific type library. You have no direct knowledge of the type library or any of its contents other than that used to create the instance of the FSO object. Languages that use compilers, such as Microsoft Visual Basic 5.0 (VB 5.0), VB 6.0, and Visual Basic for Applications (VBA), provide a way for programmers to bind early and connect to the entire type library. Microsoft Office applications can provide this information if the programmers add a reference to the library. However, if programmers choose to use the CreateObject function in VB 5.0, VB 6.0, or VBA, they have the same limitations that scripters do: Values for constants such as ForAppending aren't automatically available in the code. Fortunately, there are several ways to work around these limitations in scripts.
Good Solutions
There are two standard solutions to the constants problem. You can do research to find the integer values associated with the constants, or you can use a .wsf file.
Researching the values. The values associated with constants' names are usually documented somewhere. For example, if you look in the Windows Script (WS) 5.6 Help file (script56.chm), you'll find that the values for the I/O constants ForReading, ForWriting, and ForAppending are 1, 2, and 8, respectively. After you find the values, you can then simply define the constants as those values in your script, with code such as
Const ForAppending = 8
This solution works fairly well for objects that have few constants and that are well documented. But it doesn't work quite as well for other objects. Sometimes finding clear, accurate documentation for constants can be difficult.
If you use a type library browser, you already know a better documentation source: the component itself. Most type library browsers show you the values for enumerated constants. Unlike the information you might get from static external documentation, these values are always correct because the type library browser obtains them from the component on your system. Microsoft Office applications include an object browser as part of their VBA macro-editing support. In the sidebar "Formatting the Reports" (May 2003, InstantDoc ID 38402) from the article "AD and WMI Reporting" (InstantDoc ID 38401), you'll find an example of how to use Microsoft Excel's object browser to look for constants' values.
Prev. page  
[1]
2
3
next page