The Best Solution
Although TlbInf32 is a great tool for exploring COM objects, you shouldn't have to write custom code to get constants' values every time you want to use them in a script. For that reason, I wrote Export-Enum.wsf, which lets you find out an object's constants and associated values. All you need to do is provide the object's programmatic identifier (ProgID) or the name of the library in which the object resides.
The code in Export-Enum.wsf is similar to the code in Listing 1, except that it has additional code that helps with error handling, output handling, and shutting down applications. I added output-handling routines so that the constants' values are outputted as VBScript constant declarations (e.g., Const ForAppending = 8). You can immediately use the constant declarations by copying and pasting them into a script. The outputhandling routines also add a header so that you know what object the constant declarations are for.
I added code that shuts down the object's application because there are no standards for how applications that are also COM servers should act when you stop using them. Some applications, such as Excel, will shut down if they're invisible and have nothing to display. Other applications, such as Word, will continue to run invisibly. As the code excerpt in Listing 2 shows, Export-Enum.wsf tries to make an application exit by first invoking a generic Quit method, then setting the application's object reference to Nothing. Be aware that these techniques might not work in all cases. COM objects that live in .dll and .ocx files will always quit when the running process exits, but out-of-process executables such as Word might not. For true out-of-process executables, you might want to watch your process list in Task Manager while starting and exiting the script to determine whether the new application is left running hidden.
Export-Enum.wsf is a console script, which means you launch it from a command-shell window using CScript. Alternatively, you can invoke the wrapper script Export-Enum.cmd if you want to run Export-Enum.wsf under WScript. You can find both Export-Enum.wsf and Export-Enum.cmd on the Windows Scripting Solutions Web site. Go to http://www.windowsitpro.com/windowsscripting, enter 48494 in the InstantDoc ID text box, then click the 48494.zip hotlink. Using Export-Enum.wsf is simple. Suppose you want to see the constants available for the WshShell object. You just run the command
Export-Enum WScript.Shell
and Export-Enum.vbs will generate a long list of constant declarations, which looks like
' Constants from 'WScript.Shell'
Const WshRunning = 0
Const WshFinished = 1
.
.
.
Because VBScript parses its files completely and catalogs all constants before running a script, you can append the constant declarations to the end of an existing .vbs file just by using the command shell's redirection (>>) operator in a command such as
Export-Enum WScript.Shell >>
MyScript.vbs
If you want to use a filename instead of a ProgID to specify the target object, you use the /F switch. For example, to export Word's constants by using the ProgID, you'd run the command
Export-Enum Word.Application
If you want to use the filename, you'd include the /F switch followed by Word's TypeLib file (msword.olb) in the command
Export-Enum /F msword.olb
When you use the /F switch, you don't need to worry about hidden applications left running because Export-Enum.wsf binds directly to the library to get the constants and their values.
The Choice Is Yours
In scripting, there are usually many ways to achieve the same end. That's certainly the case when you need to find out the values for constants. Besides researching documentation, using a .wsf file, and using TlbInf32, you now have another option from which to choose: Export-Enum.wsf.
Alex K. Angelopoulos (alexangelopoulos@ hotmail.com) is a senior network engineer who does IT consulting work in Indiana. He is an MCSE, an MCP+I, and an MVP.
End of Article
Prev. page
1
2
[3]
next page -->