*

The Runny Nose

One of the cool things about COM is the fact that you don't have to reinvent the wheel for your program - just use someone else's wheel, most likely Microsoft® WheelT. For instance, Excel's charting functions and Access's reporting functions make business applications a little snazzier, don't cost anything extra (finding a system without Office installed these days is pretty rare) and are already coded and [mostly] debugged for you.

BTW, the proper term for this branch of COM is called Automation. Excel and Access (and the other Office apps) are ActiveX exes: they lead meaningful existences on their own (i.e. double click and run) but can also be used for their code.

So, how does one go about making Automatable applications do tricks? As you learned in the course book, there are a couple of ways to instantiate an object: New and CreateObject.

New is typically used for your VB created classes and dlls, but can be used to call up the Office apps programmatically, as well.

CreateObject can be used for your VB classes and dlls but is most often used in ASP programming where typing of variables is not allowed - remember "New" needs a referenced type library in order to work whereas CreateObject specifies the type library as one of its parameters. (For those of you who might be saying, "whoa - type libraries? huh??", tomorrow's COM Mini-lecture is on how COM uses the Registry and things like CLSIDs, ProgIDs and Type Libraries.)

There's another "object" function: GetObject. This one is similar to CreateObject in that it can function without a pre-referenced type library and it does create instances of classes, but there's a catch to GetObject: to make it create an instance, you have to specify a file name along with the type library. Otherwise, GetObject merely gets objects that were already in existence because of a New or CreateObject.

I'm talking about GetObject in the context of automating Office applications because, as MSDN documentation states:

You can't use GetObject to obtain a reference to a class created with Visual Basic.

The VB documentation on GetObject and CreateObject does a pretty good job of explaining when to use each and gives some examples. Automation examples usually invoke the Office applications because those apps are somewhat common, but Automation does not require them. In fact, nearly everything one does in VB involves Automation. In order have an application that can "perform tricks" (exposes its methods and properties for another app to use), it just has to have the IDispatch COM interface referenced. All VB created COM apps have this automatically. I'll be talking more about COM interfaces in Part III.

Now, for GetObject. Its primary reason for being is to retrieve existing documents, as you mentioned. If I wanted a new document I'd use New or CreateObject, personally. I reread my post and I can see where that sounds confusing. What I meant by "Otherwise, GetObject merely gets objects that were already in existence because of a New or CreateObject" is something along the lines of this:

Dim ppt As Powerpoint.Application
Set ppt = New Powerpoint.Application
'in case PowerPoint takes a while to start up
    Do While ppt Is Nothing
        Set ppt = GetObject("PowerPoint.Application")
    Loop

This works because PowerPoint is what I call a "handkerchief" application: no matter how many times New or CreateObject or GetObject is called on it, it will only ever produce a single instance, programmatically (or in your task bar if made visible). Technically, it's called "creatable multi-use". Word, on the other hand, is a "kleenex" application - creatable single-use - multiple instances allowed, programmatically and in the task bar. I've attached a little project called Runny Nose Example* that [hopefully] demonstrates this.



Internet Content Rating Association Valid CSS!

Alrak's Course Resources ©2002-2007 Karla Carter. All rights reserved. This material (including, but not limited to, Mini-lectures and Challenge Labs) may not be reproduced, displayed, modified or distributed without the express prior written permission of the copyright holder. For permission, contact Karla.