Part III Adding More Functionality to Office
Using GetObject and CreateObject in VBS
I
n VBA code, you can use GetObjectto set a reference to an existing instance of an application,
such as Word or Excel, to avoid creating numerous instances running in the background (you can
see several WINWORD.EXE processes in the Task Manager in the following figure). Typically, an
error handler detects whether GetObjectset the reference, and if not, CreateObjectis used
instead. Although the GetObjectfunction worked correctly in earlier versions of Windows, unfor-
tunately, this technique doesn’t work when you run VBS scripts in Windows Vista. Whether you use
GetObject(with a slightly different syntax), or CreateObject, and whether or not there is a run-
ning instance of an application, a new instance is created every time the script is run.
Multiple instances of Word in the Task Manager.