在保留HTML的同时替换身体的各个部位

时间:2016-01-15 12:14:24

标签: vba outlook-vba

我正在努力寻找&替换HTML正文的部分内容。我设法做到这一点,但没有使用HTMLbody所以一旦我运行宏,电子邮件的正文就变成了明文。

这是我最初尝试过的:

Sub ReplaceText()

Dim Insp As Inspector
Dim obj As Object

    Set Insp = Application.ActiveInspector
    Set obj = Insp.CurrentItem

    obj.Body = Replace(obj.Body, "TEXT TO FIND", "TEXT TO REPLACE WITH")

    Set obj = Nothing
    Set Insp = Nothing

End Sub

由于这不起作用,我试图使用WordEditor。我目前的代码没有做任何事情。

Public Sub ReplaceText()
    Dim objItem As Object
    Dim objInsp As Outlook.Inspector

    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    On Error Resume Next

    Set objItem = Application.ActiveInspector.CurrentItem
    If Not objItem Is Nothing Then
        If objItem.Class = olMail Then
            Set objInsp = objItem.GetInspector
            If objInsp.EditorType = olEditorWord Then
                Set objDoc = objInsp.WordEditor
                Set objWord = objDoc.Application
                Set objSel = objWord.Selection

                With objSel.Find
                 .Text = "This is an online meeting"
                 .Replacement.Text = "TEST TEST"
                 .Execute Replace:=wdReplaceAll
                End With

            End If
        End If
    End If

    Set objItem = Nothing
    Set objWord = Nothing
    Set objSel = Nothing
    Set objInsp = Nothing
End Sub

2 个答案:

答案 0 :(得分:0)

您需要在原始函数中将.body替换为.HTMLBody,这应该有效。

编辑 - 这是适合我的功能。

Sub ReplaceText()

Dim Insp As Inspector
Dim obj As Object

Set Insp = Application.ActiveInspector
Set obj = Insp.CurrentItem

obj.HTMLBody = Replace(obj.HTMLBody, "TEXT TO FIND", "TEXT TO REPLACE WITH")

Set obj = Nothing
Set Insp = Nothing

End Sub

答案 1 :(得分:0)

如何选择消息

Option Explicit
Sub ReplaceText()
    Dim olItem As Outlook.MailItem

    '//- Selected msg
    If Application.ActiveExplorer.Selection.Count = 0 Then
        Exit Sub
    End If

    For Each olItem In Application.ActiveExplorer.Selection
        If olItem.Class = olMail Then
            olItem.HTMLBody = Replace(olItem.HTMLBody, "TEXT TO FIND", "TEXT TO REPLACE WITH")
        End If
    Next
End Sub