Outlook add-in. How to manage element events

I am making an add-in for Outlook 2007 in C ++.

I need to capture events like create, change, or delete from Outlook items (contact, appointment, tasks and notes), but the only information / examples I found is for Visual Basic, so I don't know how to wire the event handler.

Below is the following information: http://msdn.microsoft.com/en-us/library/bb208390(v=office.12).aspx

Any help is appreciated :) Thanks

Update

Sorry for taking so long to update, I was out of town. I have some doubts / problems you may know how to help.

In my case, I am taking this project that was launched, so I am a little confused about the whole thing. I have an OutlookAddin class that comes from IDTExtensibility2. I also have this other class called AutoSync, if I would like to use all the methods when the event fires. An object of this class is initialized in OutlookAddin.cpp OnStartupComplete.

According to your post MyClass should extend from IDispEventSimpleImpl<1 /*N*/, MyClass, &__uuidof(Outlook::ItemsEvents)>

, but which one? OutlookAddin or AutoSync?

Where should I put this code?

CComPtr<Outlook::MAPIFolder> folder;
// get the folder you're interested in
CComPtr<Outlook::_Items> items;
hr = folder->get_Items(&items);
hr = MyItemEvents::DispEventAdvise(items, &__uuidof(Outlook::ItemsEvents));

typedef IDispEventSimpleImpl<1 /*N*/, MyClass, 
          &__uuidof(Outlook::ItemsEvents)> MyItemEvents;

      

I have read the links you posted, but still having these doubts ...

Update 2

This is more difficult to understand than in the first case.

So, I like the following:

OutlookAddin.h

class OutlookAddin : 
public IDTExtensibility2,
public IDispEventSimpleImpl<1, OutlookAddin, &__uuidof(Outlook::ItemEvents)>
...
BEGIN_SINK_MAP(OutlookAddin)
SINK_ENTRY_INFO(1, __uuidof(Outlook::ItemEvents), 0xf002, OutlookAddin::OnItemChange, &OnSimpleEventInfo)
END_SINK_MAP()
...
void __stdcall OnItemChange();

      

'OnSimpleEventInfo' is defined as:

extern _ATL_FUNC_INFO OnSimpleEventInfo;
_ATL_FUNC_INFO OnSimpleEventInfo = {CC_STDCALL,VT_EMPTY,0};

      

and then in OutlookAddin.cpp, the OnConnection method:

    CComPtr<Outlook::MAPIFolder> folder;
CComPtr<Outlook::_Items> items;

OutlookWorker::GetInstance()->GetNameSpacePtr()->GetDefaultFolder(olFolderContacts, &folder);
folder->get_Items(&items);
DispEventAdvise(items, &__uuidof(Outlook::ItemsEvents));

      

is "OutlookWorker :: GetInstance () -> GetNameSpacePtr ()" _NameSpacePtr, where the entire environment is saved.

The expected behavior here is to trigger the OnItemChange function from the OutlookAddin class when creating / modifying / deleting a ContactItem, but it doesn't ... I changed the structure a bit and it's all in the main OutlookAddin class. Then, in the "OnItemChange" function, I launched the "AutoSync" object that I told you about earlier.

Anyway, I am following the articles you gave me, very helpful, thanks. Do you still have any other suggestion for me?

Thank you for your patience.

+2


a source to share


1 answer


It has been a while, but you should get these element events by consulting for Folder.Items

:

CComPtr<Outlook::MAPIFolder> folder;
// get the folder you're interested in
CComPtr<Outlook::_Items> items;
hr = folder->get_Items(&items);
hr = MyItemEvents::DispEventAdvise(items, &__uuidof(Outlook::ItemsEvents));

      

If your class MyClass

comes from:

IDispEventSimpleImpl<1 /*N*/, MyClass, &__uuidof(Outlook::ItemsEvents)>

      

And MyItemEvents

:

typedef IDispEventSimpleImpl<1 /*N*/, MyClass, 
          &__uuidof(Outlook::ItemsEvents)> MyItemEvents;

      

N

identifies your receiver here. Then there is the joy of the remaining macros for customization and handler functions for implementation - I refer you to this and this article and examples dispinterface ItemsEvents

you can find at oleview.exe

.




Update 1:
If you want to receive events in AutoSync

, implement the interface there - you don't have to pass events to any particular instance. However, you know your design is best :)
I would just keep as much logic from the main addin class as possible.

The registration code is included in any method of the class that implements the events, and then it is called whenever it should start receiving events, when typedef

, for example, it would be well placed in the class declaration.


Regarding update 2:

With a quick glance, it looks mostly correct, but OnItemChange()

takes one parameter - a IDispatch

:

_ATL_FUNC_INFO AtlCallDispatch = {CC_STDCALL, VT_EMPTY, 1, {VT_DISPATCH}};

      

+1


a source







All Articles