VBA 使用 WordEditor 在电子邮件中加粗计算的句点

VBA to bold calculated period in email message using WordEditor

我正在编写一个 vba 脚本来自动执行每周一次的电子邮件任务,并且我正在使用 WordEditor 插入文本消息。在电子邮件中,有按函数 Format(DateAdd("m", -1, Now()), "Mmmm") 计算的月份,我想用粗体和红色突出显示它们。有人可以告诉我执行此操作的代码吗?

Sub EmailAllOpenPOs()

Dim ol As Outlook.Application
Dim mi As Outlook.MailItem
Dim doc As Word.Document
Dim MsgText As String


Set ol = New Outlook.Application
Set mi = ol.CreateItem(olMailItem)

With mi
    .Subject = "Purchase Orders Review & Approval Process (REVIEW & ACTION REQUIRED) as of " & Format(Now(), "d/m/yy")
    .Display
End With

Set doc = mi.GetInspector.WordEditor

MsgText = vbNewLine
doc.Range(0, 0).InsertBefore MsgText

MsgText = "Dear all," & vbNewLine & vbNewLine & _
        "There are some POs related to " & Format(DateAdd("m", -1, Now()), "Mmmm") & " and " & Format(Now(), "Mmmm") & " still open." & vbNewLine & vbNewLine & _
        "Could you please review and advise a.s.a.p. if you require finance to accrue them for this month end?"

doc.Range(0, 0).InsertBefore MsgText

Set doc = Nothing

End Sub

Set doc = Nothing之前添加:

Dim formatRng As Word.Range
Set formatRng = doc.Range.Duplicate
With formatRng
    .Find.Execute Format(DateAdd("m", -1, Now()), "Mmmm") & " and " & Format(Now(), "Mmmm")
    .Font.Bold = True
    .Font.ColorIndex = wdRed
End With
Set formatRng = Nothing

如果您只想将月份加粗和红色(不带“和”):

Dim formatRng As Word.Range
Set formatRng = doc.Range.Duplicate
With formatRng
    .Find.Execute Format(DateAdd("m", -1, Now()), "Mmmm") & " and " & Format(Now(), "Mmmm")
    With .Words(1).Font
        .Bold = True
        .ColorIndex = wdRed
    End With
    With .Words(3).Font
        .Bold = True
        .ColorIndex = wdRed
    End With
End With
Set formatRng = Nothing