Microsoft

Tracking down objects to script

Ever wonder if there are other objects you can use in your scripts to manipulate applications? Tracking them down is half the battle. This Daily Drill Down examines some of the resources you can use to find out about scripting other objects.


When you’re working with Windows Script Host, your goal is to automate or simplify a task within the Windows operating system. Since Windows Script Host and its scripting languages are object-oriented programming languages, you achieve this goal by manipulating objects.

Windows Script Host, JScript, and VBScript provide you with a number of objects that you can take advantage of to automate certain tasks within the operating system. For example, the WshNetwork object allows you to access and manipulate network connections and the FileSystemObject object provides you with access to a computer’s file system.

However, you may be wondering if there are other objects you can use in your scripts to access and manipulate applications? If so, how do you go about tracking them down? The answer to the first part of the question is yes. Many Windows applications come with objects that you can use in your scripts. For example, Internet Explorer installs several objects that you can access from within your scripts like the Browse For Folder object that I used in the RenameFiles script. Additionally, Microsoft Office installs many objects that you can access from within your scripts.

The answer to the second half of the question has two parts. First, there are many resources that will introduce you to available objects and show you how to use them. Second, there are several tools that you can use to search through your system to find objects you might be able to script. However, it’s important to keep in mind that while these resources and tools will provide you with information on the available objects, much of it is very basic. Thus, you will still have to do some experimentation in order to learn exactly how to use the objects in your scripts.

In this Daily Drill Down, I’ll examine some of the resources that you can use to learn about scripting other objects. Also, I will introduce you to some of the tools that you can use to search your system for available objects. I’ll pass on some tips and tricks you can use for learning more about objects, as well.

Studying object models
Before you can begin tracking down and scripting other objects on your own, you need a good understanding of what an object is. The best way to get a handle on this topic is to study some object models. Basically, an object model is a detailed description of the components that make up an object and how they relate to each other.

Since you probably already have some experience with using the various Windows Script Host objects, a good place to begin your study is with the object model. You can find detailed information on the object model from the Microsoft Windows Script Technologies Web site. Just follow the menu links to the Windows Script Host Downloads section and click on the Technical Paper link. This will take you to the Microsoft Windows Script Host Programmer’s Reference, which contains the Windows Script Host object model.

Note
If you’re interested in learning more about the Microsoft Office object models, you can find detailed information about the Office 97 Object model on the Microsoft Office Developer Web site. You can find detailed information about the Office 2000 Object model on the MSDN Online Library. For other applications, you may be able to find an object model in the documentation or in the online help. You can also seek out a developer’s guide from the application’s manufacturer. Be sure to check out the manufacturer’s Web site.

As you study an object model, keep in mind that your goal isn’t to memorize the information; rather, you just want to get an idea of how an object model is laid out. With this background, you’ll feel more comfortable once you begin using the tools designed for tracking down objects. You’ll come back to an object model as a reference when you actually begin scripting the object.

Studying examples
One of the best ways of learning how to use other objects in your scripts is by studying examples. Seeing how someone else has used an object from another application is very beneficial, as it will cut out a lot of the guesswork when it comes to implementing the object in your script.

For example, you can download sample scripts from the Microsoft Windows Script Technologies Web site. Just follow the links to the Windows Script Host Samples section. Once you download and extract the sample files, you’ll find several VBScript and JScript files that show you how to access the Excel object from within your scripts. For instance, the Addusers.vbs script provides you with a lot of valuable information on scripting the Excel object. Let’s take a closer look.

Figure A shows a section of code from the Addusers.vbs script. As you can see, you can launch Excel from within a script by using the Excel.Application object and the Visible method. You’ll also see how to use some of the other properties the Excel object exposes, such as the ability to open an existing spreadsheet using Workbooks.Open and the ability to access a specific cell in the spreadsheet using ActiveSheet.Range(“A2”).Activate.

Figure A
Studying the sample Addusers.vbs script gives insight on how to incorporate the Excel object in a script.


There are two other sample files that will show you more techniques for working with Excel. The Excel.vbs script will show you how to create a new spreadsheet, configure its layout, and then add data to specific cells. The Chart.vbs script will show you how to create and manipulate a chart in Excel. In addition, you can find an abundance of sample scripts to study on Clarence Washington’s Win32 Scripting Web site. Just follow the link to the VBScript repository.

Using the OLE/COM Object Viewer
As I mentioned, there are several tools that you can use to search through your system to find objects that you might be able to script. The first tool I’ll introduce you to is the free OLE/COM Object Viewer from Microsoft.

To get the OLE/COM Object Viewer, just point your browser to the Downloads page of the Microsoft COM Resources Web site and follow the OLE/COM Object Viewer link. Keep in mind that the utility hasn’t been updated in a while and is listed as working in Windows 95 and Windows NT. However, it works fine in Windows 98, Windows Me, and Windows 2000.

The installation procedure doesn’t create a shortcut to the OLE/COM Object Viewer on the Start menu. Once you download and install the OLE/COM Object Viewer, you can launch it by accessing the x:\Mstools\Bin (where x is your primary hard drive) and double-clicking the OLEView.exe file.

When you first launch it, the OLE/COM Object Viewer looks very intimidating. There are no user-friendly features in the OLE/COM Object Viewer’s interface, so you really have to know what you’re looking for when you use it. As such, you’ll get more out of the OLE/COM Object Viewer if you use it in conjunction with an object model as a reference. With this in mind, let’s take a look at the basics.

To use the OLE/COM Object Viewer to find an object that you can script, you’ll begin by locating and opening the All Objects folder in the Object Classes tree. Once you open this branch, you’ll be astounded at how many objects are on your system. However, it’s important to realize that not all of these objects can be used in a script (more on this in a moment).

Suppose you are interested in launching Word from within a script and then opening the Visual Basic for Applications Editor. To begin, you’ll locate and double-click on the Microsoft Word Application object. You’ll then look for the program ID in the right pane, as indicated by the number 1 in Figure B. You’ll use the program ID, in this case Word.Application, to reference the object from within your script. Keep in mind that you don’t need to include the number that follows the program ID, as this simply indicates the specific version. Leaving the number off the reference makes it easier to use the object in your script and will indicate that you want to use the currently installed version of the application.

Figure B
The first thing you’ll need to access an object in your script is the object’s program ID.


You’ll next locate and double-click on the IDispatch item in the tree, as indicated by the number 2 in Figure B, in order to uncover the methods and properties that belong to the Microsoft Word Application object. Keep in mind that the presence of the IDispatch item underneath the object indicates that you may be able to script the object. If there isn’t an IDispatch item, you can’t script the object. When you see the IDispatch Viewer dialog box, just click the View TypeInfo button (second button on the top row). When you see the ITypeInfo Viewer window, double-click on the Methods item in the tree, as shown in Figure C.

Figure C
The ITypeInfo Viewer window lists all the methods that belong to an object.


You can now scan through the methods to locate the ones that will launch Word and open the Visual Basic for Applications Editor—namely the Visible and ShowVisualBasicEditor methods, as shown in Figure D. While this information appears rather cryptic, it tells you that both of these methods accept Boolean values of either 1 and 0 or True and False. If you’re not sure how to interpret the information the OLE/COM Object Viewer makes available, be sure to check with an object model for a more detailed description.

Figure D
Each method’s display contains information on how to use the method.


Using the information derived from the OLE/COM Object Viewer and Word’s object model, the script to launch Word and open the Visual Basic for Applications Editor would look like the one shown in Figure E.

Figure E
Searching through the OLE/COM Object Viewer for information about launching Word and opening the Visual Basic for Applications Editor yielded this script.


Using the Object Browser
If you’re running Microsoft Office 97 or 2000, you probably know that these suites come with their own programming language called Visual Basic for Applications, which provides you with a tool called Object Viewer that was specifically designed for tracking down objects. This tool is much easier to use than the OLE/COM Object Viewer, as it comes with a friendlier interface, provides you with much more detailed information on how to use objects, and offers such convenient features as a search tool and Forward and Back buttons.

To access the Object Viewer, open any Office application and press [Alt][F11] to launch the Visual Basic for Applications Editor. You can also use the script shown in Figure E. Pull down the Tools menu and select the References command. When you do, you’ll see the References dialog box, as shown in Figure F. At this point, scroll through the list until you locate the object you want to work with in your script, select the check box, and click OK. To continue, pull down the View menu and select the Object Browser command. Alternatively, you can use the [F2] key.

Figure F
You’ll use the References dialog box to select the object you’re interested in.


When the Object Browser window appears in the Visual Basic for Applications Editor, you’ll select the object you want to study from the drop-down list at the top of the window. You can use the Search feature to look for the object to find the information you need. For example, if you were interested in learning how to open a specific worksheet in Excel from within your script, you could search for the Open method. You’ll see a window for each of the results, as shown in Figure G.

Figure G
Using the Object Browser’s Search feature makes quick work of searching for an object’s methods.


Once you select the specific item you’re interested in, you’ll find a detailed description of how to use it at the bottom of the Object Browser window. As you can see, the Open method, which is listed as a function, takes a filename as a string value and is a member of Excel.Workbooks. This means that the line of code in your script to launch Excel and open a specific worksheet would look like the one shown in Figure H.

Figure H
Searching through the Object Browser for information about launching Excel and opening a specific worksheet yielded this script.


As you use the Object Browser, keep in mind that you can use the icons to quickly identify items. For instance, the icon with a hand holding a card represents properties, the green flying book icon represents methods, and the lightening bolt represents events. While the Object Browser provides you with good information, it doesn’t hurt to consult an object model to be sure you are on the right track.

Conclusion
When you create scripts, chances are that you’re going to want to be able to work with objects other than those exposed by Windows Script Host and its native scripting languages. In this Daily Drill Down, I’ve described how you can use object models along with several tools to help you track down objects that you can use within your scripts.

About Greg Shultz

Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

Editor's Picks

Free Newsletters, In your Inbox