accesing e mail body in a outlook add-in

E

Ertan EFE

hi everyone,
I m new to outlook programming. ý m developing a outlook add-in. in the add
in ý want to do some tasks.
I want to access e-mail body and get it. when ý call the get_Body method on
actual _MailItem object it gives me a access violation error message.
code is here

HRESULT hr;
CComPtr<Outlook::MAPIFolder> spMapiFolder;
m_spExplorer->get_CurrentFolder(&spMapiFolder);

CComPtr<Outlook::_Items> spOutlookItems;
spMapiFolder->get_Items(&spOutlookItems);

CComPtr<Outlook::_MailItem> spMailItem;
hr = spOutlookItems->GetFirst((IDispatch**)&spMailItem);

CComBSTR spMessageBody;
spMailItem->get_Body(&spMessageBody)

OutputDebugString("ok....")

the program couldn't passed the spMailItem->get_Body(&spMessageBody) code
line when the the line of code executed an access violation error occured.


then ý tryed with reading some basic property of the MailItem witout passig
a bstr pointer. for example get mesaagee read status.
corresponding code is here

VARIANT_BOOL l_bIsUnread(TRUE);
hr = spMailItem->get_UnRead(&l_bIsUnread);

the program couldn't passed the hr = spMailItem->get_UnRead(&l_bIsUnread);
code line. it gives me an error message.


"Run-Time Check Failure #0 - The value of ESP was not properly saved across
a function call. This is usually a result of calling a function declared
with one calling convention with a function pointer declared with a
different calling convention."


does anyone know what is my wrong think ?
many thanks to any advise
 
D

Dmitry Streblechenko

Are you sure spMailItem is not NULL? If is not NULL, try to declare
spMailItem as IDispatch, and then *explicitly* query it for the
Outlook.MailItem interface.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
E

Ertan EFE

hi,
spMailItem is not null. I changed my code before reading your suggestion and
now it is working. The changed part of code here

IDispatchPtr spMailItem;
spOutlookItems->Item(index,(IDispatch**)&spMailItem);

Outlook::_MailItemPtr spMailItemPtr = spMailItem;
VARIANT_BOOL unread(true);
spMailItemPtr->get_UnRead(&unread);

but I did't understant why _MailItem and _MailItemPtr objecst is there. what
is the distinction between two classes

and I have another question. I didn't found documantation of the COM library
for Outlook objects.

do you have a suggestion or can you give a link for documentation ?

many thanks for your efforts.
 
D

Dmitry Streblechenko

The line

hr = spOutlookItems->GetFirst((IDispatch**)&spMailItem);

says "take spMailItem declared as MailItem and treat it as IDispatch, which
is what GetFirst() method takes". You get a perfectly valid IDispatch object
and treat it as MailItem (which is derived from IDispatch). Calling any of
the MailIem methods using early binding results in an access violation since
spMailItem points to a v-table for IDispatch, not to a v-table for MailIem
(which includes IDispatch methods and adds its MailItem specific methods).
This *may* sometimes work *if* GetFirst() actually returned a MailItem
(which is perfectly validd since it is derived from IDispatch and hence
satifies the method signature). *But* if you are running your code
out-of-proc, COM run-time performs the marshalling and return a proxy object
that implemented the IDispatch v-table, but not those of MailItem, so even
if the original method returned MailItem, COM would not know about that
since the type library only says that IDispatch is returned. Now if you call
QueryInterface (either explicitly or thee compiler does that for you), COM
proxy can retrieve the MailItem interface signarure from the type library
and return the expected v-table.
The line

Outlook::_MailItemPtr spMailItemPtr = spMailItem;

calls spMailItem.QueryInterface(__uuidof(Outlook::MailItem), ...) under the
covers, so you get the expected MailItem object.

As do the Outlook Object Model documentation, install thee Outlook VBA help
file.


Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

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