Comparing Outlook 2003 Objects

T

Tom Winter

I'm having a problem comparing objects from an Outlook 2003 COM Add-in. Take
the following code for example (it's contrived, but you get the idea):

If oOutlook.ActiveExplorer Is oOutlook.ActiveExplorer Then

MsgBox "Same"

Else

MsgBox "Different"

End If

From a VB6 COM Add-In, using the reference to the Outlook Application that
was passed into the COM Add-In, the code above will output "Different"! If
you do the same code from a regular VB6 application that uses GetObject() to
get the Outlook Application, the above code will output "Same".

Note that I never had this problem in Outlook 2002 or 2000.

I assume this is due to new security stuff in Outlook 2003, such as COM
Add-Ins being "trusted" as long as they use the Outlook Application object
passed to them. That's great, but how to I compare two objects? I need to
know if a reference to an Explorer that I have is the ActiveExplorer. I've
always used the "Is" operator to compare my reference with
Outlook.ActiveExplorer. Now that always fails. Is there any other way to do
that?
 
D

Dmitry Streblechenko \(MVP\)

This has nothing to do with Outlook security - you cannot directly compare
COM objects: every time you call oOutlook.ActiveExplorer, Outlook returns a
new instance of the Explorer object, even if it refers to the same physical
instance of the Explorer; this is just a basic COM rule.
Previous version of Outlook probably always returned the same cached object,
Outlook 2003 creates a new instance, you should not rely on either behavior.
You could try to QI the explorer for IOleWindow, then call
IOleWindow::GetWindow() to get the window handle (HWND) of the explorer. If
the two are the same, two COM objects represent the same explorer.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
T

Tom Winter

I like the idea about IOleWindow. Thanks! Have you done that reliably?

I have compared object in the past just fine. Try this VB6 Program:

Sub Main()

Dim oOutlook As Outlook.Application

Dim oEx1 As Outlook.Explorer

Dim oEx2 As Outlook.Explorer

Set oOutlook = GetObject(, "Outlook.Application")

Set oEx1 = oOutlook.ActiveExplorer

Set oEx2 = oOutlook.Explorers(1)

MsgBox ObjPtr(oEx1)
MsgBox ObjPtr(oEx2)

If oEx1 Is oEx2 Then

MsgBox "SAME"

Else

MsgBox "DIFFERENT"

End If

End Sub

For me, it prints "SAME" and the address of both objects is the same. It is
up to Outlook what object it wants to return. In this case it returns the
same object. For COM Add-Ins, it returns different ones.
 
D

Dmitry Streblechenko \(MVP\)

Yes, IOleWindow should work just fine, after all it is the same window in
the HWND sense.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top