0

I'm writing an application that captures some Outlook events, I want to capture ItemAdd event on sentMail folder for each account, for Outlook 2007 (where there is only one sent mail folder for all accounts) I'm using the following code. What changes I have to perform to make it work with Outlook 2010 (subscribe to the event for all accounts)? Any help is appreciated.

const IID IID_ItemsEvents = {0x00063077, 0x0000, 0x0000, {0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};

CComPtr<Outlook::_Application> spApplication;
hr = spApplication.CoCreateInstance(__uuidof(Outlook::Application), 0, CLSCTX_LOCAL_SERVER );

if(SUCCEEDED(hr) && spApplication)
{
    CComPtr<Outlook::_NameSpace> spSession;
    hr = spApplication->get_Session(reinterpret_cast<Outlook::_NameSpace **>(&spSession));

    if (SUCCEEDED(hr) && spSession)
    {

        CComPtr<Outlook::MAPIFolder> spSentMailsFolder;

        hr = spSession->GetDefaultFolder(Outlook::olFolderSentMail, &spSentMailsFolder);
        CComPtr<Outlook::_Items> spItems;
        spSentMailsFolder->get_Items(&spItems);

        if (SUCCEEDED(hr) && spItems)
        { 
            CComPtr<Outlook::ItemsEvents > spItem;
            CComPtr<IConnectionPointContainer> spContainer;

            HRESULT hr = spItems->QueryInterface(__uuidof(IConnectionPointContainer),reinterpret_cast<void **>(&spContainer));

            if (SUCCEEDED(hr))
            {   
                HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
                CComPtr<CItemsEventListener> spSink = new CItemsEventListener(hEvent);
                CComPtr<IConnectionPoint> spConnectionPoint;

                hr = spContainer->FindConnectionPoint(IID_ItemsEvents, &spConnectionPoint);

                if (SUCCEEDED(hr) && spConnectionPoint)
                {
                    DWORD dwCookie = 0;                                                             
                    hr = spConnectionPoint->Advise(spSink, &dwCookie);  

                    if (SUCCEEDED(hr))
                    {                                   
                        while(true)
                        {    
                            MSG Message;
                            while(PeekMessage(&Message, NULL, WM_NULL, WM_NULL, PM_REMOVE))
                            {
                                TranslateMessage(&Message);
                                DispatchMessage(&Message);
                            }   

                            DWORD dwStatus = WaitForSingleObject(hEvent, 0);
                            Sleep(1);
                        }

                        spConnectionPoint->Unadvise(dwCookie);                                  
                    }

                }
            }                                               

        }
        else
        {
            //m_LogTrace->WriteLine("\tERROR\tEchec de l'appel de la méthode get_Items");
        }
    }
    else
    {
        //m_LogTrace->WriteLine("\tERROR\tEchec de l'appel de la méthode get_Session");
    }

    spApplication.Release();
}
Kira
  • 1,153
  • 4
  • 28
  • 63

1 Answers1

0

Yu will need to keep each Items object in a list/array and run the code above for each Items object.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78