changing html email body using VC++, ATL addin?

  • Thread starter Thread starter kp
  • Start date Start date
K

kp

Hi,
I am working on com addin using VC++ 6.0, ATL, Outlook 2003.
I have an event handler OnNewInspector. It gets called when
a email message is opened. I can change email body using
pMailItem->get_Body() and pMailItem->put_Body(). But I want
to change html version of email body using pMailItem->get_HTMLBody()
and pMailItem->put_HTMLBody(). It is not working. Could you please help
me?

Thanks in advance,
KP
 
Hi,
The changed email body(html version) is not displayed.
Instead old email is displayed.

Thanks,
KP
 
Hi,
Following is the code:
It gets the HTML body for the email. And adds "This is modified".
And it puts the changed body using put_HTMLBody(). But the
added string is not displayed. Only old email body is displayed.
Could you please tell me what is wrong here?
--------------------------------------------------------------------------------------------
void __stdcall CAddIn::OnNewInspector(IDispatch *Ctrl)
{
_Inspector *pInsp = (_Inspector *)Ctrl;
IDispatch *pCurrentItem;
pInsp->get_CurrentItem(&pCurrentItem);
_MailItem *pMailItem = (_MailItem *) pCurrentItem;

BSTR body_bstr;
_bstr_t body_bstr_t;

pMailItem->get_HTMLBody(&body_bstr);
body_bstr_t = body_bstr;

int len = _tcslen(body_bstr_t);
TCHAR *newbody_str;
newbody_str = new TCHAR[len + 100];
_tcscpy(newbody_str, body_bstr_t);
_tcscat(newbody_str, "This is modified");
...
...
pMailItem->put_BodyFormat(olFormatHTML);
_tcscpy(body_bstr_t, newbody_str);
pMailItem->put_HTMLBody(body_bstr_t);

}
 
Of course it won't work: if you have an HTML string like
<html><body>test</body></html>
and you change it to
<html><body>test</body></html>This is modified

Outlook will not display "This is modified" string because it is outside of
the HTML document. You will need to either insert the new string in the
correct position or use DOM to load the HTML string, then insert a new HTML
object using the DOM.

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

Dmitry said:
Of course it won't work: if you have an HTML string like
<html><body>test</body></html>
and you change it to
<html><body>test</body></html>This is modified

Outlook will not display "This is modified" string because it is outside of
the HTML document. You will need to either insert the new string in the
correct position or use DOM to load the HTML string, then insert a new HTML
object using the DOM.

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

kp said:
Hi,
Following is the code:
It gets the HTML body for the email. And adds "This is modified".
And it puts the changed body using put_HTMLBody(). But the
added string is not displayed. Only old email body is displayed.
Could you please tell me what is wrong here?
--------------------------------------------------------------------------------------------
void __stdcall CAddIn::OnNewInspector(IDispatch *Ctrl)
{
_Inspector *pInsp = (_Inspector *)Ctrl;
IDispatch *pCurrentItem;
pInsp->get_CurrentItem(&pCurrentItem);
_MailItem *pMailItem = (_MailItem *) pCurrentItem;

BSTR body_bstr;
_bstr_t body_bstr_t;

pMailItem->get_HTMLBody(&body_bstr);
body_bstr_t = body_bstr;

int len = _tcslen(body_bstr_t);
TCHAR *newbody_str;
newbody_str = new TCHAR[len + 100];
_tcscpy(newbody_str, body_bstr_t);
_tcscat(newbody_str, "This is modified");
...
...
pMailItem->put_BodyFormat(olFormatHTML);
_tcscpy(body_bstr_t, newbody_str);
pMailItem->put_HTMLBody(body_bstr_t);

}
 
Hi,
One more problem:
When I open the Inspector window, for the first time,
the changed email body is shown in preview pane. But
in Inspector window Old email body is shown. When I
come second time, both preview pane and Inspector window
show changed email body. Could you please tell me what I
need to do for getting the changed email, for first time,
in Inspector window?

Thanks,
KP said:
Hi,
Thanks a lot.
- KP

Dmitry said:
Of course it won't work: if you have an HTML string like
<html><body>test</body></html>
and you change it to
<html><body>test</body></html>This is modified

Outlook will not display "This is modified" string because it is outside of
the HTML document. You will need to either insert the new string in the
correct position or use DOM to load the HTML string, then insert a new HTML
object using the DOM.

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

kp said:
Hi,
Following is the code:
It gets the HTML body for the email. And adds "This is modified".
And it puts the changed body using put_HTMLBody(). But the
added string is not displayed. Only old email body is displayed.
Could you please tell me what is wrong here?
--------------------------------------------------------------------------------------------
void __stdcall CAddIn::OnNewInspector(IDispatch *Ctrl)
{
_Inspector *pInsp = (_Inspector *)Ctrl;
IDispatch *pCurrentItem;
pInsp->get_CurrentItem(&pCurrentItem);
_MailItem *pMailItem = (_MailItem *) pCurrentItem;

BSTR body_bstr;
_bstr_t body_bstr_t;

pMailItem->get_HTMLBody(&body_bstr);
body_bstr_t = body_bstr;

int len = _tcslen(body_bstr_t);
TCHAR *newbody_str;
newbody_str = new TCHAR[len + 100];
_tcscpy(newbody_str, body_bstr_t);
_tcscat(newbody_str, "This is modified");
...
...
pMailItem->put_BodyFormat(olFormatHTML);
_tcscpy(body_bstr_t, newbody_str);
pMailItem->put_HTMLBody(body_bstr_t);

}
 
You *really* need to post the relevant snippets of your code...

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

kp said:
Hi,
One more problem:
When I open the Inspector window, for the first time,
the changed email body is shown in preview pane. But
in Inspector window Old email body is shown. When I
come second time, both preview pane and Inspector window
show changed email body. Could you please tell me what I
need to do for getting the changed email, for first time,
in Inspector window?

Thanks,
KP said:
Hi,
Thanks a lot.
- KP

Dmitry said:
Of course it won't work: if you have an HTML string like
<html><body>test</body></html>
and you change it to
<html><body>test</body></html>This is modified

Outlook will not display "This is modified" string because it is
outside of
the HTML document. You will need to either insert the new string in the
correct position or use DOM to load the HTML string, then insert a new
HTML
object using the DOM.

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

Hi,
Following is the code:
It gets the HTML body for the email. And adds "This is modified".
And it puts the changed body using put_HTMLBody(). But the
added string is not displayed. Only old email body is displayed.
Could you please tell me what is wrong here?
--------------------------------------------------------------------------------------------
void __stdcall CAddIn::OnNewInspector(IDispatch *Ctrl)
{
_Inspector *pInsp = (_Inspector *)Ctrl;
IDispatch *pCurrentItem;
pInsp->get_CurrentItem(&pCurrentItem);
_MailItem *pMailItem = (_MailItem *) pCurrentItem;

BSTR body_bstr;
_bstr_t body_bstr_t;

pMailItem->get_HTMLBody(&body_bstr);
body_bstr_t = body_bstr;

int len = _tcslen(body_bstr_t);
TCHAR *newbody_str;
newbody_str = new TCHAR[len + 100];
_tcscpy(newbody_str, body_bstr_t);
_tcscat(newbody_str, "This is modified");
...
...
pMailItem->put_BodyFormat(olFormatHTML);
_tcscpy(body_bstr_t, newbody_str);
pMailItem->put_HTMLBody(body_bstr_t);

}
 
Hi,
I am repeating the same code (slightly modified) below:
body_bstr_t1 is initialized to "<HTML> \n <BODY> This is modified
</BODY> \n </HTML>";
For the first time, in the preview pane, the string "This is modified"
is shown. But in
the Inspector window, for the first time, it is showing old email.
Second time onwards,
preview pane and Inspector window are showing "This is modified"
string.
Am I missing something? I am working with this code only.

Thanks
KP

--------------------------------------------------------------------------------------------------
void __stdcall CAddIn::OnNewInspector(IDispatch *Ctrl)
{
_Inspector *pInsp = (_Inspector *)Ctrl;
IDispatch *pCurrentItem;
pInsp->get_CurrentItem(&pCurrentItem);
_MailItem *pMailItem = (_MailItem *) pCurrentItem;


BSTR body_bstr;
_bstr_t body_bstr_t1;

pMailItem->get_HTMLBody(&body_bstr);
body_bstr_t1 = "<HTML> \n <BODY> This is modified
</BODY> \n </HTML>";
pMailItem->put_HTMLBody(body_bstr_t1);
}
---------------------------------------------------------------------------------------------------
 
Hi,
I do not want to save the message, as it becomes a
permanent change. I want the changes to be visible only
for that session. So I can not use MailItem.Save(). Is
there any other way?

Thank you very much,
KP
 
Getting Outlook to display something which wasn't first persisted is always
tricky . User can click "Save", plus Outlook will prompt to save the chnages
when the inspector is closed. Sounds like you need a custom form, which can
display pretty much anything you want even if it is totally unrelated to an
exisiting message.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Hi,
Thanks a lot. Please suggest me, if there is any other simpler way
other than custom form?

Thanks,
KP
 
Back
Top