VBA 从 Outlook 邮件中检索 HTMLBody

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16144149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:46:00  来源:igfitidea点击:

VBA retrieve HTMLBody from Outlook mail

vbaoutlook

提问by Trace

First I create an email via Outlook:

首先,我通过 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

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.

通过代码,我想根据 ID 检索值。这对我来说似乎是最干净的方式,我并不特别喜欢“拆分”方法,因为它是一种硬编码;不是很动态,有点不可靠。

Unfortunately when I retrieve the HTML body, I cannot retrieve the original HTML, as it is distorted by Outlook:

不幸的是,当我检索 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 

On debug.print, this is what I get (only putting the last line of the Format):

在 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>

I would like to retrieve the original HTML that I put in the HTMLBody.

我想检索我放在 HTMLBody 中的原始 HTML。

回答by Maciej Los

2 ways:

2种方式:

1)parsing text - several things to do (not recommended: hard-coding)

1)解析文本 - 要做的几件事(不推荐:硬编码)

All what you need is to parse text, but MSDN shows how to do it using InStrfunction. I would strongly suggest to use RegExto parse html text. Note: reference to MS VBScript Regular Expressions x.xis needed.

您需要做的就是解析 text,但 MSDN 显示了如何使用InStr函数进行解析。我强烈建议使用RegEx来解析 html 文本。注意:需要参考MS VBScript 正则表达式 xx

Simple Regular Expression Tutorial for Excel VBA

Excel VBA的简单正则表达式教程

2)using UserProperitesof MailItem object (recommended)

2)使用MailItem对象的UserProperites推荐

If MailItem doesn't contains your propert(y)ies, than there is nothing to do ;)

如果 MailItem 不包含您的属性(y)ies,则无需执行任何操作;)

How to: Add custom property

如何:添加自定义属性