我正在尝试使用 Outlook vba 提取传入的电子邮件并将其保存在文件中。似乎无法将尸体提取出来。从 MailItem 获取主题或地址没有问题。我知道有 3 种格式,所以下面的测试代码将它们全部打印出来。但是,他们都错了。3 种格式(plain、RTF 或 HTML)的内容都不匹配我所知道的正文中的内容。
实际的正文内容是字符串“测试内容”。
第一个 MsgBox 不打印任何内容。
第二个 MsgBox 打印 ???????????????????????????????????????????????? ??????????????????
第三个 MsgBox 打印以下内容:
<HTML>
<HEAD>
<META NAME="Generator" CONTENT="MS Exchange Server version
14.02.5004.000">
<TITLE></TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
</BODY>
</HTML>
任何帮助将不胜感激。
这是我的测试代码:
Sub SaveMail(objMail As MailItem)
'writes the message into a file, for later processing
Dim strID As String
Dim subjectStr As String
Dim bodyStr1 As String
Dim bodyStr2 As String
Dim bodyStr3 As String
Dim senderEmail As String
Dim subject As String
strID = objMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
subjectStr = objMail.subject
senderEmail = objMail.SenderEmailAddress
subject = objMail.subject
bodyStr1 = objMail.Body
bodyStr2 = objMail.RTFBody
bodyStr3 = objMail.HTMLBody
MsgBox (bodyStr1)
MsgBox (bodyStr2)
MsgBox (bodyStr3)
'method 1 which doesn't work for the body
Set fs = CreateObject("Scripting.FileSystemObject")
Set tso = fs.CreateTextFile("C:\Users\Charles\ps1\outlook\" & subjectStr & "-body.txt", True)
tso.WriteLine ("Sender: " & senderEmail)
tso.WriteLine ("Subject: " & subject)
tso.WriteLine ("Start Body: ")
'tso.WriteLine (objMail.Body)
'tso.WriteLine (objMail.RTFBody)
tso.WriteLine (objMail.HTMLBody)
tso.WriteLine ("End Body: ")
tso.Close
'method 2 which doesn't work for the body
objMail.SaveAs "C:\Users\Charles\ps1\outlook\" & subjectStr & ".txt", olTXT
Set objMail = Nothing
End Sub