Outlook VBA 将富文本转换为 HTML 格式 [英] Outlook VBA convert Rich Text to HTML format

查看:119
本文介绍了Outlook VBA 将富文本转换为 HTML 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的交换服务器空间有限,因此我想将 Outlook 2007 收件箱中的所有选定邮件转换为 HTML 格式,因为当涉及图像时,这些邮件小于对应的富文本格式.我有以下代码可以完成这项工作,但格式无处不在,图像变成无法读取的附件,并且大小没有改变.

I have limited space in my exchange server, so I want to convert all selected messages in my Outlook 2007 inbox to HTML format as they are smaller than their Rich Text format equivalents when images are involved. I have the following code which kind of does the job, but the formatting goes all over the place and the images become undreadable attachments, and the size doesn't change.

Public Sub ConvertHTML()

    Dim selItems As Selection
    Dim myItem As Object

' Set reference to the Selection.
    Set selItems = ActiveExplorer.Selection

' Loop through each item in the selection.
    For Each myItem In selItems
        myItem.Display
        myItem.BodyFormat = olFormatHTML
        myItem.Close olSave
    Next

    MsgBox "All Done. Email converted to HTML.", vbOKOnly, "Message"

    Set selItems = Nothing

End Sub

如果我手动执行此操作:- 打开富文本电子邮件,编辑消息,更改为 HTML,保存并关闭,然后格式保持不变,图像保持嵌入状态并且消息大小减小.如何在 VBA 中复制它?我已经检查了 BodyFormat 文档,它确实警告格式丢失,所以它可能是不可能的.谢谢

If I do it manually:- Open Rich Text email, Edit message, change to HTML, Save and close, then the formatting remains, the image stays embedded and the message size is reduced. How can I replicate this in VBA? I have checked the BodyFormat documentation and it does warn of formatting loss so it may just not be possible. thanks

推荐答案

如果有关于属性 BodyFormat 和三种 body 格式的明确文档,我从来没有发现过.

If there is any clear documentation on property BodyFormat and the three body formats, I have never discovered it.

从 Outlook 2003 甚至更早的版本开始,MailItem 就具有 Body 和 HtmlBody 属性.在 Outlook 2010 之前,我找不到属性 RTFBody 的提及.我检查过的大多数电子邮件都有正文和 HtmlBody.我从未见过 RTFBody.Outlook 2003 可以选择创建一个 RTF 正文,但显然,除了作为 Html 正文之外,没有其他方法可以存储它.我从未尝试过创建 RTF 正文,因为我的朋友很少使用 Outlook,而且我怀疑他们的电子邮件包是否支持 RTF.

A MailItem has had properties Body and HtmlBody since Outlook 2003 and perhaps earlier. I can find no mention of property RTFBody before Outlook 2010. Most emails I have examined have both Body and HtmlBody. I have never seen a RTFBody. Outlook 2003 had the option of creating a RTF body but, apparently, no way of storing it other than as an Html body. I have never tried creating a RTF body because few of my friends use Outlook and I doubt their email packages support RTF.

我知道如果你修改 HtmlBody,Outlook 会修改 Body 来匹配.这不是一个非常复杂的修改;据我所知,新的正文只是删除了所有 Html 标签的新 HtmlBody.

I know that if you amend the HtmlBody, Outlook will amend Body to match. It is not a very sophisticated amendment; as far as I can tell, the new Body is just the new HtmlBody with all the Html tags removed.

当您将正文格式从 RTF 更改为 Html 时会发生什么?Outlook 是否删除了 RTF 正文,以便您看到幕后始终存在的错误 Html 正文?Outlook 是否严重尝试从 RTF 正文创建 Html 正文?我不知道,但也许我们可以找到.

What happens when you change the body format from RTF to Html? Does Outlook delete the RTF body so you see the faulty Html body that was always there behind the scenes? Does Outlook attempt, badly, to create an Html body from the RTF body? I do not know but perhaps we can find out.

下面的宏将 Html 正文保存为桌面上的 Html 文件.我的浏览器完美地显示了这些文件.请在您的一些带有 RTF 正文的电子邮件上尝试使用此宏.目的是发现是否有一个好的 Html 正文隐藏在 RTF 正文后面.如果有,我建议你试试:

The macro below saves Html bodies as Html files on the desktop. My browser displays those files perfectly. Please try this macro on some of your emails with RTF bodies. The objective is to discover if there is a good Html body hiding behind the RTF body. If there is, I suggest you try:

  • 将 Html 正文保存为字符串.
  • 将正文格式更改为 Html.
  • 清除 RTF 正文.
  • 从字符串中恢复 Html 正文.

.

Option Explicit
Sub CheckHtmlBody()

  ' Technique for locating desktop from answer by Kyle:
  ' http://stackoverflow.com/a/17551579/973283

  Dim Exp As Outlook.Explorer
  Dim InxS As Long
  Dim Path As String

  Path = CreateObject("WScript.Shell").SpecialFolders("Desktop")

  Set Exp = Outlook.Application.ActiveExplorer

  If Exp.Selection.Count = 0 Then
    Debug.Print "No emails selected"
  Else
    For InxS = 1 To Exp.Selection.Count
      With Exp.Selection(InxS)
        If .HtmlBody <> "" Then
          Call PutTextFileUtf8(Path & "\TestHtml" & InxS & ".htm", .HtmlBody)
        End If
      End With
    Next
  End If

End Sub
Public Sub PutTextFileUtf8(ByVal PathFileName As String, ByVal FileBody As String)

  ' Outputs FileBody as a text file (UTF-8 encoding without leading BOM)
  ' named PathFileName

  ' Needs reference to "Microsoft ActiveX Data Objects n.n Object Library"
  ' I have only tested with version 6.1.

  '  1Nov16  Copied from http://stackoverflow.com/a/4461250/973283
  '          but replaced literals with parameters

  Dim BinaryStream As Object
  Dim UTFStream As Object

  Set UTFStream = CreateObject("adodb.stream")

  UTFStream.Type = adTypeText
  UTFStream.Mode = adModeReadWrite
  UTFStream.Charset = "UTF-8"
  UTFStream.LineSeparator = adLF
  UTFStream.Open
  UTFStream.WriteText FileBody, adWriteLine

  UTFStream.Position = 3 'skip BOM

  Set BinaryStream = CreateObject("adodb.stream")
  BinaryStream.Type = adTypeBinary
  BinaryStream.Mode = adModeReadWrite
  BinaryStream.Open

   'Strips BOM (first 3 bytes)
  UTFStream.CopyTo BinaryStream

  UTFStream.Flush
  UTFStream.Close
  Set UTFStream = Nothing

  BinaryStream.SaveToFile PathFileName, adSaveCreateOverWrite
  BinaryStream.Flush
  BinaryStream.Close
  Set BinaryStream = Nothing

End Sub

这篇关于Outlook VBA 将富文本转换为 HTML 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆