0

我正在尝试使用 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
4

1 回答 1

0

尝试将 "tso.WriteLine (objMail.HTMLBody)" 替换为 "tso.WriteLine (bodyStr3)" 。

我无法解释为什么,(除了它可能会强制对象类型为字符串)但是在以下(Kludgy!)代码片段中通过一个变量对我有用(在我的完整代码模块中,它被暂时粘贴到

With OutMail
.Display
.To = Email
.CC = CC
.BCC = BCC"
End with

部分。)

  Dim HTMLBodyFULL
  Dim objFSO As Object
  Dim strFile As String
  Dim objFile
  HTMLBodyFULL = .HTMLBody
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  strFile = "C:\TempDelete\HTMLBody.txt"
  Set objFile = objFSO.CreateTextFile(strFile, True)
  objFile.Write HTMLBodyFULL
  objFile.Close
  Set objFile = Nothing
  Set objFSO = Nothing

要在您的代码中使用,我相信将您的 objMail 粘贴到 .HTMLBody 前面(并创建 TempDelete 文件夹)

我尝试了一种或两种其他方法来获取一封特定模板电子邮件的全文,所有这些方法都未能提供整个文本。Debug.print 似乎只给出了 200 行,而 Msgbox 给出了你所拥有的。24 行电子邮件模板的总 HTML 行数变成了 641 行!

于 2015-10-20T23:06:29.403 回答