Capturing outlook events not working with C++

C

Chuck Bohling

I'm trying to capture outlook events using the object model and C++. I
successfully set an IConnectionPoint::Advise sink for outlook's
ApplicationEvents. I get Startup, NewMail, etc. events just as expected.
However, when I try to set an Advise sink for a MAPIFolder's FolderEvents,
nothing happens. No events are sent. I've checked to make sure I have the
correct folder. Everything seems ok. There are no errors in the code that
sets the sink. In fact, my sink's QueryInterface is called, with
IID_IDispatch. It looks like it should work but doesn't. Anyone have any
thoughts. Here the code. Thanks.

_ApplicationPtr app = g_dispatch; // from outlook
_NameSpacePtr ns = app->GetNamespace("MAPI");
MAPIFolderPtr folder = ns->GetDefaultFolder(olFolderInbox);
MAPIFolderPtr subFolder = folder->Folders->Item(1);

_FoldersPtr folders = subFolder->Folders;

IConnectionPointContainerPtr connectionPointContainer = folders;
if (connectionPointContainer != NULL)
{
IConnectionPoint *connPoint = NULL;
connectionPointContainer->FindConnectionPoint(__uuidof(FoldersEvents),
&connPoint);
IConnectionPointPtr connectionPoint = connPoint;

if (connectionPoint != NULL)
{
ULONG cookie;
SQFoldersEventsPtr events = new SQFoldersEvents;
connectionPoint->Advise(events, &cookie);
}
}
 
D

Dmitry Streblechenko \(MVP\)

Do you keep the folders variable alive at all times? Or is it a local
variable?

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

Chuck Bohling

You got it. The connectionpoint smartptr's were local and went out of scope.
I did the same with the applicationevents but for some reason, those
smartptr's stayed around. I discovered the problem by trying to unadvise in
some other piece of code.

Thanks

BTW, outlookspy is fabulous!!
 
D

Dmitry Streblechenko \(MVP\)

Yep, Outlook used to always return the same instance of the Application
object, that's why it worked. I think they finally fixed it in 2003, so your
code trapping the Application events would break there too.

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