none
MailItem Set Body not keeping the text formating in the message RRS feed

  • Question

  • Hi,

    I have a requirement to copy text from a RTF document to email mesage, keeping the formatting intact.

    I am using the Outlook 2003:

        COMPointer< Outlook_2003::_Items > DraftItems( pDraft->get_Items(), false );
        TASSERT( DraftItems != NULL );

        COMPointer< IDispatch > NewItem( DraftItems->Add(), false );
        TASSERT( NewItem != NULL );

        COMPointer< Outlook_2003::_MailItem > pMailItem;
        TASSERT(
            SUCCEEDED(
                NewItem->QueryInterface(
                    __uuidof(Outlook_2003::_MailItem),
                    (void**)&pMailItem.p
                )
            )
        );
        TASSERT( pMailItem != NULL );
        //earlier opened the rtf doc and copied the text to clipboard,
        AnsiString sMessageBody = Clipboard()->AsText ;
        pMailItem->set_BodyFormat(olFormatRichText) ;
        pMailItem->set_Body( sMessageBody ) ;
        //some more code to set other message properties.............
        pMailItem->Display() ;

    The set_Body call above is loosing some of the text formatting above. For instance the text color, bullets etc. is not retained as in the rtf document.
    The set_Body() call above is loosing some of the text formatting above. For instance the text color, bullets etc. is not retained as in the rtf document.

    Any help appreciated.

    Thanks

    Monday, March 19, 2012 7:42 AM

Answers

  • If the store natively supports HTML, you can copy the PR_HTML property and the underlying message store will update the PR_RTF_COMPRESSED property appropriately.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    • Marked as answer by ank_s Thursday, March 22, 2012 7:25 AM
    Monday, March 19, 2012 2:42 PM

All replies

  • Try to use this function: Converting RTF to HTML in VB.NET the Easy Way

    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved

    Monday, March 19, 2012 9:00 AM
  • I am using C++ on Borland Builder 6.0, so could not use VB.NET

    The link says that the "Outlook did not support rich text". Is that correct?

    Monday, March 19, 2012 9:40 AM
  • Outlook does support rich text, but if you set PR_BODY or PR_BODY_W you won't get rich text. That property is for plain text.
     
    You'd need to set PR_RTF_COMPRESSED (0x10090102) to the actual rich text content and also to make sure that PR_RTF_IN_SYNC (0x0E1F000B) is set.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "ank_s" <=?utf-8?B?YW5rX3M=?=> wrote in message news:d411c5f1-c2db-40c1-8813-1a1d7e65e206...

    I am using C++ on Borland Builder 6.0, so could not use VB.NET

    The link says that the "Outlook did not support rich text". Is that correct?


    Ken Slovak MVP - Outlook
    Monday, March 19, 2012 1:29 PM
    Moderator
  • If the store natively supports HTML, you can copy the PR_HTML property and the underlying message store will update the PR_RTF_COMPRESSED property appropriately.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    • Marked as answer by ank_s Thursday, March 22, 2012 7:25 AM
    Monday, March 19, 2012 2:42 PM
  • Ken,

    Can I set PR_RTF_COMPRESSED using MailItem object, as I have MailItem and not IMessage.

    • Edited by ank_s Wednesday, March 21, 2012 10:59 AM Missing words
    Wednesday, March 21, 2012 10:21 AM
  • Dmitry,

    How to check if my store natively supports HTML? I am using Outlook 2003.


    • Edited by ank_s Wednesday, March 21, 2012 10:59 AM Missing words
    Wednesday, March 21, 2012 10:23 AM
  • to get IMessage from MailItem, access it .MAPIOBJECT property and QI it to IMessage.
    Wednesday, March 21, 2012 12:00 PM
  • The Outlook MailItem object has an RTFBody property.
     
    That's a binary array property, so you could get your RTF text and convert it into a binary array from a string and set that property to your array. In that case you wouldn't need to use IMessage or other MAPI properties.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "ank_s" <=?utf-8?B?YW5rX3M=?=> wrote in message news:fa17c957-f139-4fc3-b04c-0c6753386f25...

    Ken,

    Can I set PR_RTF_COMPRESSED using MailItem object, as I have MailItem and not IMessage.


    Ken Slovak MVP - Outlook
    Wednesday, March 21, 2012 1:49 PM
    Moderator
  • You can get PR_STORE_SUPPORT_MASK (0x340D0003) from a folder or store object and test it to see if the STORE_UNICODE_OK flag (PT_LONG) is set: http://support.microsoft.com/kb/884671

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "ank_s" <=?utf-8?B?YW5rX3M=?=> wrote in message news:8b1b29d0-4563-4f7c-bba8-a8d13572542b...

    Dmitry,

    How to check if my store natively supports HTML? I am using Outlook 2003.



    Ken Slovak MVP - Outlook
    Wednesday, March 21, 2012 1:49 PM
    Moderator
  • The Outlook MailItem object has an RTFBody property.
    That's a binary array property, so you could get your RTF text and convert it into a binary array from a string and set that property to your array. In that case you wouldn't need to use IMessage or other MAPI properties.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "ank_s" <=?utf-8?B?YW5rX3M=?=> wrote in message news:fa17c957-f139-4fc3-b04c-0c6753386f25...

    Ken,

    Can I set PR_RTF_COMPRESSED using MailItem object, as I have MailItem and not IMessage.


    Ken Slovak MVP - Outlook

    Is MailItem's RTFBody property available in Outlook 2003? I am using Outlook 2003.
    Wednesday, March 21, 2012 1:59 PM
  • Is MailItem's RTFBody property available in Outlook 2003? I am using Outlook 2003.
    Wednesday, March 21, 2012 1:59 PM
  • No, in Outlook 2007 and below, Extended MAPI (C++ or Delphi) and Redemption are your only options.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!


    Wednesday, March 21, 2012 2:11 PM
  • Or CDO 1.21 for Outlook 2003 (or even 2007).

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Dmitry Streblechenko _MVP_" <=?utf-8?B?RG1pdHJ5IFN0cmVibGVjaGVua28gX01WUF8=?=> wrote in message news:85b7e5a1-0af6-4f8e-a978-d5076759e49a...

    No, in Outlook 2007 and below, Extended MAPi (C++ or Delphi) and Redemption are your only options.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!


    Ken Slovak MVP - Outlook
    Wednesday, March 21, 2012 2:17 PM
    Moderator
  • CDO 1.21 will not decompress the PR_RTF_COMPRESSED property.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Wednesday, March 21, 2012 2:22 PM
  • If the store natively supports HTML, you can copy the PR_HTML property and the underlying message store will update the PR_RTF_COMPRESSED property appropriately.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Thanks, I was able to see the correct formatting after setting the Body format to olFromatHTML and using the set_HTMLBody(). Converted the RTF to HTML, read the HTML text, and finally used the set HTMLBody.

    However I am having problems setting Unicode character using HTML body. The scenario is my text is non English (Unicode), the HTML opens correctly. But when I try setting HTMLBody, the whole message contains invalid character?

    Why is this happening? Do I need to set any other message property for Unicode body?

    Thursday, March 22, 2012 7:38 AM
  • HTMLBody, unlike the actual binary data in PR_HTML, is Unicode.

    How do you convert between teh two?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Thursday, March 22, 2012 1:57 PM
  • The code snippet is below:

        pMailItem->set_Subject(WideString(sMsgSubject)) ;
        AnsiString m_sTemplateDocPath = "C:\\Documents and Settings\\DEVELOPER1\\Desktop\\AIT1.html";
        int         iFileHandle;
        int         iFileLength;
        wchar_t     *pszBuffer;

        iFileHandle = FileOpen(m_sTemplateDocPath, fmOpenRead);
        iFileLength = FileSeek(iFileHandle,0,2);
        FileSeek(iFileHandle,0,0);
        pszBuffer = new wchar_t[iFileLength+1];
        FileRead(iFileHandle, pszBuffer, iFileLength);
        FileClose(iFileHandle);
        pMailItem->set_BodyFormat(olFormatHTML) ;
        pMailItem->set_HTMLBody( pszBuffer ) ;

    Is something missing?

    Friday, March 23, 2012 6:18 AM
  • I am confused - you are reading file contents to a Unicode (wchar_t) buffer.

    Is HTML file really in the Unicode (UTF16, 2 bytes per char) format?

    File size is in bytes, but you use it to set the length of a Unicode string in characters (which are 2 bytes per char).


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Friday, March 23, 2012 9:36 PM