You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Outlook VBA替换邮件内容时保留格式问题求助

Fix Outlook VBA Replace Losing Formatting in Email Body

Hey Benjamin, I’ve dealt with this exact frustration before—using the .Body property strips all formatting because it only works with plain text. Luckily, there are two reliable ways to replace text while keeping your font sizes, bolding, hyperlinks, and other styles intact:

Option 1: Use .HTMLBody for HTML-Formatted Emails

If your template email is in HTML format, you can directly modify the HTML content instead of the plain text body. This preserves all HTML-based formatting:

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.Session.OpenSharedItem(Path)

With OutMail
    .To = Receiver(ActiveCell.Offset(, 11))
    .Subject = "End of contract" & ActiveCell.Value & " " & _
               ActiveCell.Offset(, 3).Value & " " & ActiveCell.Offset(, 2).Value & " - " & ActiveCell.Offset(, 6).Value
    ' Replace in HTMLBody instead of Body to keep formatting
    .HTMLBody = Replace(.HTMLBody, "FIRST NAME; NAME", ActiveCell.Offset(, 11).Value)
    .FlagRequest = "Follow up"
    .FlagDueBy = ReminderDate
    ' Save or send the email as needed
    .Save
End With

Set OutMail = Nothing
Set OutApp = Nothing

Option 2: Use the Word Object Model (Most Reliable)

This method works for both HTML and Rich Text emails, as it uses Outlook’s built-in Word editor to manipulate the body content just like you would in a Word document. It’s the best choice for preserving all formatting:

' Define constants if you don't have the Word library referenced
Const wdReplaceAll = 2

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.Session.OpenSharedItem(Path)

' You need to display the email first to access the Word editor
OutMail.Display
' Hide the window if you don't want the user to see it
OutMail.Visible = False

' Get the Word document object for the email body
Set WordDoc = OutMail.GetInspector.WordEditor

' Use Word's Find/Replace to swap the placeholder without losing formatting
With WordDoc.Content.Find
    .Text = "FIRST NAME; NAME"
    .Replacement.Text = ActiveCell.Offset(, 11).Value
    .MatchCase = False
    .MatchWholeWord = True
    .Execute Replace:=wdReplaceAll
End With

' Finish setting up the email
With OutMail
    .To = Receiver(ActiveCell.Offset(, 11))
    .Subject = "End of contract" & ActiveCell.Value & " " & _
               ActiveCell.Offset(, 3).Value & " " & ActiveCell.Offset(, 2).Value & " - " & ActiveCell.Offset(, 6).Value
    .FlagRequest = "Follow up"
    .FlagDueBy = ReminderDate
    .Save
End With

' Clean up objects
Set WordDoc = Nothing
Set OutMail = Nothing
Set OutApp = Nothing

Key Notes:

  • The Word Object Model method requires calling .Display first—Outlook doesn’t load the Word editor until the email is displayed. Setting .Visible = False keeps it hidden from the user.
  • If you don’t want to reference the Microsoft Word Object Library in your VBA project, just use the numeric value 2 instead of wdReplaceAll.
  • Both methods will preserve your original formatting, so your hyperlinks, bold text, and font sizes stay exactly as they were in the template.

内容的提问来源于stack exchange,提问作者Benjamin Frza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:15:36