title: VBA retrieve HTMLBody from Outlook mail
body: <p>First I create an email via Outlook: </p>
  
  <pre><code>Sub CreateHTMLMail()
  'Creates a new e-mail item and modifies its properties.
  
  Dim olApp As Outlook.Application
  Dim objMail As Outlook.MailItem
  Set olApp = Outlook.Application
  'Create e-mail item
  Set objMail = olApp.CreateItem(olMailItem)
  
  Dim sHTML_Open              As String
  Dim sHTML_Introduction      As String
  Dim sHTML_Goodbye           As String
  Dim sHTML_Close             As String
  Dim sHTML_Process_Date      As String
  Dim sHTML_Processor         As String
  Dim sHTML_Issuer            As String
  Dim sHTML_Details           As String
  
  Dim sHTML_Body              As String
  
  sHTML_Open = "&lt;HTML&gt;&lt;BODY&gt;"
  sHTML_Introduction = "Hi team,&lt;BR/&gt;&lt;BR/&gt;" &amp; _
                          "Data is ready to process. Please find details as below.&lt;BR/&gt;"
  sHTML_Process_Date = "&lt;P ID='PROCESSDATE'&gt;28 February 2013&lt;/P&gt;"
  sHTML_Processor = "&lt;P ID='PROCESSOR'&gt;AKSHAY&lt;/ID&gt;&lt;/P&gt;"
  sHTML_Issuer = "&lt;P ID='ISSUER'&gt;DATAGROUP.COM&lt;/ID&gt;&lt;/P&gt;"
  sHTML_Details = "&lt;P ID='DETAILS'&gt;" &amp; _
                      "&lt;UL&gt;" &amp; _
                          "&lt;LI&gt;Fimta23456 09:00:00 flor345&lt;/LI&gt;" &amp; _
                          "&lt;LI&gt;Fimta23456 09:00:00 flor345&lt;/LI&gt;" &amp; _
                      "&lt;/UL&gt;" &amp; _
                  "&lt;/P&gt;&lt;BR/&gt;"
  sHTML_Goodbye = "Thanks"
  sHTML_Close = "&lt;/BODY&gt;&lt;/HTML&gt;"
  
  sHTML_Body = sHTML_Open &amp; sHTML_Introduction &amp; sHTML_Process_Date &amp; sHTML_Processor &amp; sHTML_Issuer &amp; _
            sHTML_Details &amp; sHTML_Goodbye &amp; sHTML_Close
  
  With objMail
     'Set body format to HTML
     .BodyFormat = olFormatHTML
     .To = "Kim Gysen"
     .Subject = "data remit file"
     .HTMLBody = sHTML_Body
     .Display
  End With
  End Sub
  </code></pre>
  
  <p>Via code, I want to retrieve values based on ID. 
  This seemed the cleanest way for me, I don't particulary like the "split" method because it's kind of hard coding; not very dynamic and kinda unreliable.  </p>
  
  <p>Unfortunately when I retrieve the HTML body, I cannot retrieve the original HTML, as it is distorted by Outlook: </p>
  
  <pre><code>Sub Get_OL()
  
  Dim oFolder                 As MAPIFolder
  Dim oItem                   As Variant
  
  Dim sHTML_Body              As String
  Dim sHTML_Process_Date      As String
  Dim sHTML_Processor         As String
  Dim sHTML_Issuer            As String
  Dim sHTML_Details           As String
  
  Dim oExcel              As Object
  Dim oBook               As Workbook
  
  Set oExcel = CreateObject("Excel.Application")
  Set oBook = oExcel.Workbooks.Add
  
  
  'Access the outlook inbox folder
  
  Set oFolder = GetNamespace("MAPI").PickFolder
  
  'On error resume next usually not to use, but feteching emails may give unexpected errors
  On Error Resume Next
  For Each oItem In oFolder.Items
      If TypeOf oItem Is Outlook.MailItem Then
          If oItem.Subject Like "*data remit file*" Then
              'Turn off on error resume next asap
              On Error GoTo 0
              sHTML_Body = oItem.HTMLBody
              Debug.Print sHTML_Body
  
              Exit For
          End If
      End If
  Next oItem
  
  End Sub 
  </code></pre>
  
  <p>On debug.print, this is what I get (only putting the last line of the Format): </p>
  
  <pre><code>&lt;/o:shapelayout&gt;&lt;/xml&gt;&lt;![endif]--&gt;&lt;/head&gt;&lt;body lang=EN-GB link=blue vlink=purple&gt;&lt;div class=WordSection1&gt;&lt;p class=MsoNormal&gt;Hi team,&lt;br&gt;&lt;br&gt;Data is ready to process. Please find details as below.&lt;br&gt;&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p&gt;28 February 2013&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p id=PROCESSOR&gt;AKSHAY&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p id=ISSUER&gt;DATAGROUP.COM&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;ul type=disc&gt;&lt;li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l0 level1 lfo1'&gt;Fimta23456 09:00:00 flor345&lt;o:p&gt;&lt;/o:p&gt;&lt;/li&gt;&lt;li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l0 level1 lfo1'&gt;Fimta23456 09:00:00 flor345&lt;o:p&gt;&lt;/o:p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p class=MsoNormal&gt;&lt;br&gt;Thanks&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>I would like to retrieve the original HTML that I put in the HTMLBody. </p>