【问题标题】:VBA retrieve HTMLBody from Outlook mailVBA 从 Outlook 邮件中检索 HTMLBody
【发布时间】:2013-04-15 04:19:28
【问题描述】:

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

【问题讨论】:

  • 这封电子邮件在CreateHTMLMail 和您查看它之间的位置是什么?上面的代码中缺少一些步骤。
  • 抱歉回复晚了。我使用 CreateHTMLMail 创建邮件,将其发送给自己,然后使用 Get_OL 检索它。我本来希望检索与最初创建它时相同的 HTML。
  • 我问的原因是我几乎测试了您的确切代码,并且能够完全按照创建的方式检索 HTML。你在换货吗?或者使用类似 gmail 之类的东西?
  • 我使用 Outlook。会不会和 RTF 设置有关?

标签: vba outlook


【解决方案1】:

两种方式:

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

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

Simple Regular Expression Tutorial for Excel VBA

2) 使用 MailItem 对象的UserProperites推荐

如果 MailItem 不包含您的属性(y),则无事可做;)

How to: Add custom property

【讨论】:

  • 感谢您的回答,虽然我不再处理这个问题了。不过,也许您的回答对其他人会有所帮助。
猜你喜欢
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
相关资源
最近更新 更多