【问题标题】:VSTO 2007 outlook addin crashesVSTO 2007 Outlook 插件崩溃
【发布时间】:2013-05-18 08:47:49
【问题描述】:

我正在使用 COM 创建 VSTO 2007 Outlook Addin。它从 Outlook 中读取所有邮件项,并将未投递的邮件项标记(类别)为未投递。

我正在使用以下代码将未送达的邮件项目标记为未送达。以读取颜色标记的代码有时会崩溃。请提出问题。

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

                                                                             }

                                                             }

                                            }

                            }

            }

提前致谢。

【问题讨论】:

  • 什么代码用颜色标出?如果您需要帮助,请进行格式化。另外,为什么要将智能指针与标准指针混合使用?您应该将 LPDISPATCH 更改为 CComPtr 否则,您将丢失 Release 调用。

标签: com outlook-addin outlook-2007


【解决方案1】:

您需要检查 spExplorer 是否为空。 Outlook 可以在没有资源管理器的情况下打开。

【讨论】:

  • 尽管 spExplorer 不为空,但 hrGetSelectedItem = spExplorer->get_Selection(&spOlSel);正在崩溃。当我选择文件夹中的所有邮件项目时,只有这个函数调用崩溃了。
  • 您可以访问 OutlookSpy 中的选择吗?单击 OutlookSpy 工具栏上的资源管理器按钮,选择选择属性,单击浏览。
猜你喜欢
  • 2019-01-21
  • 2011-08-27
  • 2011-05-28
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
相关资源
最近更新 更多