-->

Programmatically change font properties in email b

2019-09-16 16:19发布

问题:

I have been successfully programming this in PowerPoint VBA but haven't been able to make it work on Outlook.

  1. I have an email ready to be sent in Outlook 2013
  2. I want to scan the body of the email for bold text (i.e., bold characters) and change its color to red
  3. (nice to have) Exclude from the macro the signature

I tried several attempts with "Substitute", "if"-loop but no success. Thanks a lot for putting me on the right track.


The following code converts the color of the body but does not discriminate for bold words. Any ideas?

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

    ' Add reference to Word library
    ' in VBA Editor, Tools, References
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    On Error Resume Next

'Reference the current Outlook item
    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


            ' replace the With block with your code
                   With objSel
                   ' Formatting code goes here
                        '.Font.Size = 18
                        If .Font.Bold = True Then
                            .Font.Color = wdColorBlue
                        End If
                        .Font.Color = wdColorRed
                        '.Font.Italic = True
                        '.Font.Name = "Arial"
                   End With

            End If
        End If
    End If

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

回答1:

First of all, I'd suggest starting from the Getting Started with VBA in Outlook 2010 article in MSDN.

You can use the HTMLBody property of Outlook items to get the HTML content of the message body or use the Word object model to get the job done. The WordEditor property of the Inspector class returns an instance of the Document class from the WOM (Word object model). See Chapter 17: Working with Item Bodies for more information.