0
votes

I am creating VSTO 2007 outlook Addin using COM . It reads all the mailtems from outlook and marks(categories) the undelivered mail items as undelivered.

I am using below code for marking the undelivered mail item as undelivered. The code marked in read colour crashes sometimes. Please suggest me the problem.

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

                                                                             }

                                                             }

                                            }

                            }

            }

Thanks in advance.

1
What code marked in color? Please make a formatting effort if you need help. Also, why do you mix smart pointers with standard ones? you should change LPDISPATCH to CComPtr<IDispatch> otherwise, you're missing Release calls. - Simon Mourier

1 Answers

0
votes

You need to check that spExplorer is not null. Outlook can be open with no Explorers.