none
Converting RTF body to HTML format without losing formatting RRS feed

  • Question

  • I have a macro I use to add a line to the top of any currently opened email. It inserts the contents of the clipboard (a copied work order number), the current date, and the name of the currently logged-in user, all in bold red text.

    The macro usually works fine as most infoming mails are HTML, except that some incoming emails are in RTF format, this macro tags it fine but wipes out all that formatting; especially frustrating when you're working with alot of tables.

    I want to fix this macro to either:

    1. Change the body to HTML format without losing the formatting or;

    2. Insert the string into the top of the email without having to change the format. Only the string formatting is currently written in HTML, see my (terrible) code below (please don't laugh):

    Sub TagEmail()
    
    ' **Tags an email with the work order number, current date, and username**
    ' This will need the Microsoft Forms 2.0 Object Library to work
    ' To add the library, go to Tools --> References, then click Browse
    ' Go to C:\Windows\System32 and add FM20.DLL
    ' For easy access to this macro, open an email and right-click the quick-access bar and select "Customize Quick Access Toolbar"
    ' Then select "Macros" from the dropdown menu and add a button for the macro
    Dim myname As Outlook.NameSpace
    Set myNameSpace = Application.GetNamespace("MAPI")
    Dim MyData As DataObject
          Set MyData = New DataObject
          Dim sClipText As String
          ' Get data from the clipboard.
          MyData.GetFromClipboard
          ' Assign clipboard contents to string variable.
          sClipText = MyData.GetText(1)
          ' Insert the tag
    On Error GoTo Errhandler
    Set myitem = Application.ActiveInspector.CurrentItem
    
    currentbody = myitem.HTMLBody
    myinitials = "ATP"
    myitem.HTMLBody = "<font color=#FF0000 size=4><b>" & sClipText & " " & Date & " " & myNameSpace.currentuser & "</b></font></P>" & currentbody
    Exit Sub
    Errhandler:
    MsgBox ("You need to open the email before running this script")
    Exit Sub
    End Sub

    • Edited by Anthony602 Tuesday, February 7, 2012 4:46 PM
    Tuesday, February 7, 2012 4:23 PM

Answers

  • You can set the BodyFormat property to olFormatHTML, but your mileage may vary - sometimes it works fine, sometimes not all formatting is correctly converted, especially when going from HTML to RTF.


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

    Wednesday, February 8, 2012 6:12 PM
  • Oh, and the RTF objects won't be converted to HTML images if you set the BodyFormat 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, February 8, 2012 6:18 PM

All replies

  • You can modify the RTF body using the MailItem.RtfBody property, but it is a lot more painful. Plus RTF body cannot be set using late binding (Outlook bug).


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

    Tuesday, February 7, 2012 4:33 PM
  • It does look painful, is there any other way?

    I noted that If i go and edit the message and to the Format Text ribbon, I can select HTML as the format without losing any of my tables, is there any way emulate this through VBA?

    Wednesday, February 8, 2012 5:12 PM
  • You can set the BodyFormat property to olFormatHTML, but your mileage may vary - sometimes it works fine, sometimes not all formatting is correctly converted, especially when going from HTML to RTF.


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

    Wednesday, February 8, 2012 6:12 PM
  • Oh, and the RTF objects won't be converted to HTML images if you set the BodyFormat 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, February 8, 2012 6:18 PM