M
Mark Wilson
I have a requirement to access a MailItem from the Explorer context when a
custom button is clicked in an Outlook 2007 native COM Addin.
The code in the Invoke method of the button click event is as follows:
///////////////////
Outlook::_ExplorerPtr pExplorer = NULL;
pExplorer = Application->ActiveExplorer();
Outlook::SelectionPtr pSelection = NULL;
pSelection = pExplorer->Selection;
variant_t vtIndex;
vtIndex.vt = VT_INT;
vtIndex.intVal = 1;
IDispatch* pIDisp = NULL;
pIDisp = pSelection->Item(&vtIndex);
Outlook::_MailItem * pMailItem = NULL;
HRESULT hRes = pIDisp->QueryInterface(__uuidof(Outlook::_MailItem),
(void**)&pMailItem);
BSTR bsTemp = SysAllocString(L"");
pMailItem->get_Subject(&bsTemp);
///////////////////
A single message is selected in a folder when the button is clicked.
Everything seems to work fine. hRes returns as S_OK and pMailItem is not
NULL. Outlook crashes on the get_Subject call. It will also crash on any
other access to the MailItem such as
bstr_t bsBody = pMailItem->Body;
A typical crash message is "Unhandled exception at 0xffffeffc in
OUTLOOK.EXE: 0xC0000005: Access violation reading location 0xffffeffc."
For simplicity all error checking in the above code has been removed.
What is the correct way to access a single item that has been selected in an
Explorer view?
custom button is clicked in an Outlook 2007 native COM Addin.
The code in the Invoke method of the button click event is as follows:
///////////////////
Outlook::_ExplorerPtr pExplorer = NULL;
pExplorer = Application->ActiveExplorer();
Outlook::SelectionPtr pSelection = NULL;
pSelection = pExplorer->Selection;
variant_t vtIndex;
vtIndex.vt = VT_INT;
vtIndex.intVal = 1;
IDispatch* pIDisp = NULL;
pIDisp = pSelection->Item(&vtIndex);
Outlook::_MailItem * pMailItem = NULL;
HRESULT hRes = pIDisp->QueryInterface(__uuidof(Outlook::_MailItem),
(void**)&pMailItem);
BSTR bsTemp = SysAllocString(L"");
pMailItem->get_Subject(&bsTemp);
///////////////////
A single message is selected in a folder when the button is clicked.
Everything seems to work fine. hRes returns as S_OK and pMailItem is not
NULL. Outlook crashes on the get_Subject call. It will also crash on any
other access to the MailItem such as
bstr_t bsBody = pMailItem->Body;
A typical crash message is "Unhandled exception at 0xffffeffc in
OUTLOOK.EXE: 0xC0000005: Access violation reading location 0xffffeffc."
For simplicity all error checking in the above code has been removed.
What is the correct way to access a single item that has been selected in an
Explorer view?