Excel VBA - 如何在保持原始值的同时更新ReplyAll.HTMLBody

问题描述 投票:0回答:2

我在尝试更新 mailItem.ReplyAll.HTMLBody 时遇到问题。我似乎无法在不删除以前的值的情况下向正文添加新文本。下面的代码会生成一个电子邮件项目,其中仅包含以前的 HTMLBody,而不包含新的“Hello world”文本。

mMail.BodyFormat = olFormatHTML
On Error Resume Next
With mMail.ReplyAll
        .HTMLBody = "Hello world" & .HTMLBody
        .Display
End With
On Error GoTo 0

如何在保留原始 HTMLBody 的同时添加到 ReplyAll 电子邮件的 HTMLBody?

excel vba outlook
2个回答
0
投票

首先,请记住,对于

HTMLBody
属性,您处理的是 HTML 网页,因此 HTML 格式应该格式正确。

其次,在调用 ReplyAll 方法后尝试拆分新项目,该方法会根据原始消息创建对所有原始收件人的回复,并返回代表回复的新

MailItem
对象。

Dim reply as Outlook.MailItem

On Error Resume Next

Set reply = mMail.ReplyAll
With reply
        .HTMLBody = "<b>Hello world<b>"
        .Display
End With

如果您需要在 HTML 文档表示的消息正文中插入任何文本,则需要找到开始的

<body>
标记并在其后面插入文本。


0
投票

原始代码应该足够了,除非缺少部分会产生影响。

尝试移动

.Display
,以便在编辑之前看到 HtmlBody。

Option Explicit

Private Sub testActiveInspector()

Dim currItem As Object
Dim replyMail As MailItem

Set currItem = ActiveInspector.CurrentItem

If currItem.Class = olMail Then
    
    currItem.BodyFormat = olFormatHTML
    
    Set replyMail = currItem.ReplyAll
    
    With replyMail
        .Display
        .HtmlBody = "Hello world" & .HtmlBody
    End With

End If

End Sub


Private Sub testSelection()

Dim currItem As Object
Dim replyMail As MailItem

Set currItem = ActiveExplorer.Selection(1)

If currItem.Class = olMail Then
    
    currItem.BodyFormat = olFormatHTML
    
    Set replyMail = currItem.ReplyAll
    
    With replyMail
        .Display
        .HtmlBody = "Hello world" & .HtmlBody
    End With

End If

End Sub

当后面跟着创建邮件的代码时,您应该删除

On Error Resume Next
。适当地解决任何错误。

在 99.9999% 的其他情况下,您应该删除

On Error Resume Next

© www.soinside.com 2019 - 2024. All rights reserved.