title: Outlook Add-in. How to manage Items Events
body: <p>I'm doing an add-in for Outlook 2007 in C++.</p>
  
  <p>I need to capture the events like create, change or delete from the Outlook Items (Contact, Appointment, Tasks and Notes) but the only information/examples I've found are for Visual Basic so I don't know how to connect the event handler.</p>
  
  <p>Here is some information related: <a href="http://msdn.microsoft.com/en-us/library/bb208390(v=office.12).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/bb208390(v=office.12).aspx</a></p>
  
  <p>Any help is welcome :) Thanks</p>
  
  <p><strong>Update</strong></p>
  
  <p>Sorry for taking this long to update, I've been out of town. I have some doubts/problems that you may know how to help.</p>
  
  <p>In my case, I'm taking this project that was started so I'm a bit confused about all this. I have the class OutlookAddin that derives from IDTExtensibility2. I also have this other class, called AutoSync, were I'd like to do all the methods when the event fires and so. An object of this class is initialized in OutlookAddin.cpp OnStartupComplete.</p>
  
  <p>According to your post MyClass should extends from <code>IDispEventSimpleImpl&lt;1 /*N*/, MyClass, &amp;__uuidof(Outlook::ItemsEvents)&gt;</code> but which one of them? OutlookAddin or AutoSync ?</p>
  
  <p>Where I should put this code also? </p>
  
  <pre><code>CComPtr&lt;Outlook::MAPIFolder&gt; folder;
  // get the folder you're interested in
  CComPtr&lt;Outlook::_Items&gt; items;
  hr = folder-&gt;get_Items(&amp;items);
  hr = MyItemEvents::DispEventAdvise(items, &amp;__uuidof(Outlook::ItemsEvents));
  
  typedef IDispEventSimpleImpl&lt;1 /*N*/, MyClass, 
            &amp;__uuidof(Outlook::ItemsEvents)&gt; MyItemEvents;
  </code></pre>
  
  <p>I've read the links you posted but still having these doubts...</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>This is more complicated to understand than I though in a first instance.</p>
  
  <p>So I have like this: </p>
  
  <p>OutlookAddin.h</p>
  
  <pre><code>class OutlookAddin : 
  public IDTExtensibility2,
  public IDispEventSimpleImpl&lt;1, OutlookAddin, &amp;__uuidof(Outlook::ItemEvents)&gt;
  ...
  BEGIN_SINK_MAP(OutlookAddin)
  SINK_ENTRY_INFO(1, __uuidof(Outlook::ItemEvents), 0xf002, OutlookAddin::OnItemChange, &amp;OnSimpleEventInfo)
  END_SINK_MAP()
  ...
  void __stdcall OnItemChange();
  </code></pre>
  
  <p>'OnSimpleEventInfo' is defined like:</p>
  
  <pre><code>extern _ATL_FUNC_INFO OnSimpleEventInfo;
  _ATL_FUNC_INFO OnSimpleEventInfo = {CC_STDCALL,VT_EMPTY,0};
  </code></pre>
  
  <p>then in OutlookAddin.cpp, OnConnection method:</p>
  
  <pre><code>    CComPtr&lt;Outlook::MAPIFolder&gt; folder;
  CComPtr&lt;Outlook::_Items&gt; items;
  
  OutlookWorker::GetInstance()-&gt;GetNameSpacePtr()-&gt;GetDefaultFolder(olFolderContacts, &amp;folder);
  folder-&gt;get_Items(&amp;items);
  DispEventAdvise(items, &amp;__uuidof(Outlook::ItemsEvents));
  </code></pre>
  
  <p>being 'OutlookWorker::GetInstance()->GetNameSpacePtr()' the _NameSpacePtr where all the environment is kept.</p>
  
  <p>The expected behaviour here is to fire the function 'OnItemChange' from OutlookAddin class when an ContactItem is created/edited/deleted but that's not happening... I changed a little bit the structure to everything is in the main class OutlookAddin. Then on the function 'OnItemChange' I'll start the object of 'AutoSync' that I told you before.</p>
  
  <p>Anyway I'm following the articles you gave me, really useful, thank you. Do you still have any other suggestion for me?</p>
  
  <p>Thanks your patience.</p>