在非默认文件夹上捕获ItemAdd事件

时间:2014-02-25 12:50:36

标签: c++ outlook outlook-2010

我正在编写一个捕获一些Outlook事件的应用程序,我想在每个帐户的sentMail文件夹中捕获ItemAdd事件,对于Outlook 2007(所有帐户只有一个已发送的邮件文件夹)我正在使用以下码。我必须执行哪些更改才能使其与Outlook 2010一起使用(订阅所有帐户的事件)?任何帮助表示赞赏。

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();
}

1 个答案:

答案 0 :(得分:0)

Yu需要将每个Items对象保存在列表/数组中,并为每个Items对象运行上面的代码。