Subscribe to Windows IT Pro
December 08, 2003 12:00 AM

Gain Functionality with Controls

ActiveX COM components
Windows IT Pro
InstantDoc ID #40883
Rating: (0)
Downloads
40883.zip

ActiveX COM controls (aka components) are useful tools that you can use in your scripts. For example, one Microsoft control called DSOleFile lets you read and modify the file properties of Microsoft Office documents on computers that don't have a licensed copy of Office installed on them. Another Microsoft control called ScriptPW lets you enter a password in a script without having that password displayed on screen. ActiveX COM controls don't have to be Microsoft components. Many third-party controls are also available, such as ServerObjects' WaitFor component. This component lets you pause a script for a specified amount of time. Let's take a look at how you can use DSOleFile, ScriptPW, and WaitFor in your scripts.

DSOleFile
In Windows Server 2003 and Windows XP, you can use the DSOleFile control, which lets you write scripts that read and modify the summary properties of Office documents (e.g., Microsoft Word files, Microsoft Excel files), without needing to automate Office directly. This functionality is useful if you need to manipulate the properties of such files but you haven't installed Office on the computer on which you're working. This situation typically occurs on servers. Many systems administrators don't want to install Office on servers because they want to limit interactive use and prevent the propagation of viruses. With the DSOleFile component, you can manipulate Office documents' properties without buying a license for Office, so you have more flexibility in where you run your scripts.

You can download the DSOleFile control's self-extracting executable, dsofile.exe, from Microsoft's Web site at http://support.microsoft.com/?kbid=224351. When the executable runs, the installation program installs the control in the location you specify and automatically registers it. The installation program creates two registry keys—HKEY_CLASSES_ROOT\DSOleFile.PropertReader and HKEY_CLASSES_ROOT\DSOleFile.PropertyReader.1—along with their associated subkeys, value names, and values.

Scant documentation exists for the DSOleFile control. In addition to the aforementioned Web site, the Object Browser in Word (or another Office application) provides information about the component's functionality. The reference you need to add to the Object Browser is DS: OLE Document Properties 1.4 Object Library. If you're unfamiliar with how to use the Object Browser, see the Web-exclusive sidebar "How to Examine Components with Word's Object Browser," http://www.winnetmag.com/windowsscripting, InstantDoc ID 40884.

Another place you can find information about the component's functionality is in the \dsofile\vbtest folder. This folder contains a DS: OLE Document Properties demonstration project and workspace that you can open in Visual Studio (VS) or another IDE that can read Visual Basic (VB). If you want to look at some of the VB code in this project but you don't have VS or another IDE, you can open the FileProp.frm file in that folder. This file can be confusing because it describes Windows forms for onscreen display, but toward the bottom is some VB code that provides a glimpse of what the developers intended for DSOleFile.

The script DocPropTester.vbs, which Listing 1 shows, demonstrates how to use the DSOleFile control with VBScript code. This script reads some of the available properties from a Word file, then adds a custom property to that file.

DocPropTester.vbs begins by declaring the variables and initializing the strFile variable with the pathname of a Word document. The script then creates a DSOleFile PropertyReader object and stores a reference to that object in the objFilePropReader variable. The script calls the PropertyReader object's GetDocumentProperties method, which returns a DocumentProperties object, and stores a reference to the DocumentProperties object in the objDocProp variable. As the code at callout A in Listing 1 shows, you use the DocumentProperties object to read the Word document's properties. If you've worked with file properties before, these properties will be familiar to you. They include such properties as Name, Author, and WordCount, whose values specify the document's name, author, and word count, respectively.

Next, the script uses the CustomProperties object's Add method to add a new custom property called TestProp to the file and add a value for that property. VBScript's MsgBox function then displays all the previously retrieved constants, one on each line. When the property has no value, the script displays a blank line.

Finally, a For Each...Next statement loops through all the custom properties and displays in a separate message box each custom property's name and value. Before displaying a custom property's value, the script converts the value to a string in case it was stored as something else.

If you want to change the value of an existing property, you simply need to include a line such as

objDocProp.Comments = "AGLN"

in the script. This code changes the Comments property's value to the string AGLN. To run DocPropTester.vbs from the command-shell window, you use the command:

cscript.exe DocPropTester.vbs

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.