VSTO 2007展望加入崩溃

时间:2013-05-18 08:47:49

标签: com outlook-addin outlook-2007

我正在使用COM创建VSTO 2007 outlook Addin。它从outlook中读取所有mailtems,并将未传递的邮件项目标记(分类)为未送达。

我使用下面的代码将未送达的邮件标记为未送达。以读取颜色标记的代码有时会崩溃。请告诉我这个问题。

HRESULT hrGetSelectedItem;

            LPDISPATCH spOlSelectedItem  = NULL;                

            CComPtr<Outlook::_Explorer> spExplorer; 

            //Locating the selected item

            CComPtr<Outlook::Selection> spOlSel; 

            if(m_spApp)

            {

                            //Get the Currently Active Explorer on the top of Desktop

                            hrGetSelectedItem         =  m_spApp->ActiveExplorer(&spExplorer);

                            if(SUCCEEDED(hrGetSelectedItem))

                            {

                                            hrGetSelectedItem = spExplorer->get_Selection(&spOlSel);

                                            if(FAILED(hrGetSelectedItem))

                                            {   

                            MessageBox(NULL,GetStringFromTable(IDS_SELECTITEM),MSGBOX_HEADER, MB_OK|MB_ICONINFORMATION);

                                                            return ;

                                            }

                                            iMailIndex+=1;

                                            VARIANT covIndex;

                                            covIndex.vt = VT_I4;

                                            covIndex.lVal = iMailIndex;

                                            if(spOlSel)

                                            {

                                                            hrGetSelectedItem = spOlSel->Item(covIndex,&spOlSelectedItem);

                                                             CComQIPtr <Outlook::_MailItem> spMailItem;

                                                             if(spOlSelectedItem)

                                                             {

                                                                             hrGetSelectedItem = spOlSelectedItem->QueryInterface(&spMailItem);//Get The selected item

                                                                             if(spMailItem)

                                                                             {

                                                                                             spMailItem->put_Categories(L"Undelivered");

                                                                                             spMailItem->Save();

                                                                             }

                                                             }

                                            }

                            }

            }              LPDISPATCH spOlSelectedItem  = NULL;                

            CComPtr<Outlook::_Explorer> spExplorer; 

            //Locating the selected item

            CComPtr<Outlook::Selection> spOlSel; 

            if(m_spApp)

            {

                            //Get the Currently Active Explorer on the top of Desktop

                            hrGetSelectedItem         =  m_spApp->ActiveExplorer(&spExplorer);

                            if(SUCCEEDED(hrGetSelectedItem))

                            {

                                            hrGetSelectedItem = spExplorer->get_Selection(&spOlSel);

                                            if(FAILED(hrGetSelectedItem))

                                            {   

                                                            MessageBox(NULL,GetStringFromTable(IDS_SELECTITEM),MSGBOX_HEADER, MB_OK|MB_ICONINFORMATION);

                                                            return ;

                                            }

                                            iMailIndex+=1;

                                            VARIANT covIndex;

                                            covIndex.vt = VT_I4;

                                            covIndex.lVal = iMailIndex;

                                            if(spOlSel)

                                            {

                                                            hrGetSelectedItem = spOlSel->Item(covIndex,&spOlSelectedItem);

                                                             CComQIPtr <Outlook::_MailItem> spMailItem;

                                                             if(spOlSelectedItem)

                                                             {

                                                                             hrGetSelectedItem = spOlSelectedItem->QueryInterface(&spMailItem);//Get The selected item

                                                                             if(spMailItem)

                                                                             {

                                                                                             spMailItem->put_Categories(L"Undelivered");

                                                                                             spMailItem->Save();

                                                                             }

                                                             }

                                            }

                            }

            }

提前致谢。

1 个答案:

答案 0 :(得分:0)

您需要检查spExplorer是否为空。 Outlook可以在没有探索者的情况下打开。