VBA检索HTMLBody从Outlook邮件

问题描述:

首先我通过Outlook创建电子邮件:VBA检索HTMLBody从Outlook邮件

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 = "<HTML><BODY>" 
sHTML_Introduction = "Hi team,<BR/><BR/>" & _ 
         "Data is ready to process. Please find details as below.<BR/>" 
sHTML_Process_Date = "<P ID='PROCESSDATE'>28 February 2013</P>" 
sHTML_Processor = "<P ID='PROCESSOR'>AKSHAY</ID></P>" 
sHTML_Issuer = "<P ID='ISSUER'>DATAGROUP.COM</ID></P>" 
sHTML_Details = "<P ID='DETAILS'>" & _ 
        "<UL>" & _ 
         "<LI>Fimta23456 09:00:00 flor345</LI>" & _ 
         "<LI>Fimta23456 09:00:00 flor345</LI>" & _ 
        "</UL>" & _ 
       "</P><BR/>" 
sHTML_Goodbye = "Thanks" 
sHTML_Close = "</BODY></HTML>" 

sHTML_Body = sHTML_Open & sHTML_Introduction & sHTML_Process_Date & sHTML_Processor & sHTML_Issuer & _ 
      sHTML_Details & sHTML_Goodbye & 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 

通过代码,我想要检索基于ID值。 这对我来说似乎是最干净的方式,我不特别喜欢“分割”方法,因为它是一种硬编码;不是很有活力,有点不可靠。

不幸的是,当我取回HTML的身体,我无法找回原来的HTML,因为它是由Outlook扭曲:

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 

在debug.print,这是我得到什么(只把最后一行格式):

</o:shapelayout></xml><![endif]--></head><body lang=EN-GB link=blue vlink=purple><div class=WordSection1><p class=MsoNormal>Hi team,<br><br>Data is ready to process. Please find details as below.<br><br><o:p></o:p></p><p>28 February 2013<o:p></o:p></p><p id=PROCESSOR>AKSHAY<o:p></o:p></p><p id=ISSUER>DATAGROUP.COM<o:p></o:p></p><ul type=disc><li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l0 level1 lfo1'>Fimta23456 09:00:00 flor345<o:p></o:p></li><li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l0 level1 lfo1'>Fimta23456 09:00:00 flor345<o:p></o:p></li></ul><p class=MsoNormal><br>Thanks<o:p></o:p></p></div></body></html> 

我想检索我放入HTMLBody的原始HTML。

+0

这封电子邮件在'CreateHTMLMail'和你正在看的时候在哪里?上面的代码中缺少那些步骤。 – enderland 2013-04-26 20:54:06

+0

对不起,延迟回复。我使用CreateHTMLMail创建邮件,然后将它发送给我自己,然后使用Get_OL检索它。我希望检索与我最初创建相同的HTML。 – Trace 2013-04-29 12:49:18

+0

我问的原因是我测试了几乎你的确切代码,并能够完全按照创建的方式检索HTML。你在交换吗?或者使用类似gmail等的东西? – enderland 2013-04-29 13:09:17

2种方式:

1)解析文本 - 几件事情要做(不建议:硬编码)

所有你需要的是parse text,但MSDN展示了如何做它使用InStr函数。我强烈建议使用RegEx解析html文本。注意:参考需要MS VBScript正则表达式x.x

Simple Regular Expression Tutorial for Excel VBA

2)使用UserProperites的MailItem对象(推荐

如果的MailItem不包含您PROPERT(Y)IES,比没有什么做的;)

How to: Add custom property

+0

感谢您的回答,虽然我不再处理这个问题。尽管如此,也许你的答案对其他人有帮助。 – Trace 2014-01-06 13:19:17