gpt4 book ai didi

VBA 从 Outlook 邮件中检索 HTMLBody

转载 作者:行者123 更新时间:2023-12-01 03:57:21 27 4
gpt4 key购买 nike

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

最佳答案

2种方式:

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

您只需要parse text ,但 MSDN 展示了如何使用 指令 功能。我强烈建议使用 RegEx解析html文本。注意:需要引用 MS VBScript 正则表达式 x.x。

Simple Regular Expression Tutorial for Excel VBA

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

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

How to: Add custom property

关于VBA 从 Outlook 邮件中检索 HTMLBody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16144149/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com