Accessing a Outlook MailItem from ActiveExplorer->Selection

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?
 
C

Colbert Zhou [MSFT]

Hello Mark,

I am researching on this and I will report back as soon as possible. By the
way, Merrey Christmas and Happy New Year to you! :)


Best regards,
Ji Zhou
Microsoft Online Community Support
 
D

Dmitry Streblechenko

Firstly, you should not allocate the string (no calls to SysAllocString).
Secondly, can you access non-string fields, such as Size?
--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 
M

Mark Wilson

Hi Dmitry.

If I remove the SysAllocString for bsTemp, it still crashes, however

Outlook::OlObjectClass olClass = pMailItem->Class;

returns olMail.

But both of these crash Outlook.
long lSize = pMailItem->Size;
BOOL bUnread = pMailItem->UnRead;
 
M

Mark Wilson

Hi Ji.

Thanks for the code you sent me.

Your code didn't compile because I'm using a different #import directive for
the object library. However, using some raw_ methods did fix the issue.

-------------------------
Outlook::_ApplicationPtr Application = NULL;
Application = g_pParentApplication;
Outlook::_Explorer* pExplorer = NULL;
HRESULT hr = Application->raw_ActiveExplorer(&pExplorer);

Outlook::Selection* pSelection = NULL;
hr = pExplorer->get_Selection(&pSelection);

VARIANT vtIndex;
vtIndex.vt = VT_INT;
vtIndex.intVal = 1;
IDispatch* pIDisp = NULL;
hr = pSelection->raw_Item(vtIndex,&pIDisp);

Outlook::_MailItem * pMailItem = NULL;
HRESULT hRes = pIDisp->QueryInterface(__uuidof(Outlook::_MailItem),
(void**)&pMailItem);

BSTR bsTemp;
pMailItem->get_Subject(&bsTemp);

MessageBoxW(NULL,bsTemp, L"Subject", MB_OK);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top