Matt
Matt

Reputation: 15061

VBA to format selected text in Outlook

I want to highlight text in an email and format it to font consolas and indent it once.

I have tried this but get an error:

Sub Code()

    Selection.Font.Name = "Consolas"
    Selection.Paragraphs.Indent

End Sub

Run-time error '429':

ActiveX component can't create object

Upvotes: 1

Views: 3396

Answers (2)

niton
niton

Reputation: 9179

You can use WordEditor to edit selected text in mail:

Private Sub Code()

    ' Mail must be in edit mode - compose, reply, forward
    ' If reading mail then under Actions | Edit Message

    ' Select some text

    Dim objDoc As Object
    Dim objSel As Object

    Set objDoc = ActiveInspector.WordEditor
    Set objSel = objDoc.Windows(1).Selection

    objSel.Font.name = "Consolas"
    objSel.Paragraphs.Indent

End Sub

Code with validations:

Sub FormatSelection()

    ' With extra validation for troubleshooting

    ' Code in Outlook

    ' Mail must be in edit mode - compose, reply, forward
    ' If reading mail then under Actions | Edit Message

    ' Select some text

    Dim myInspector As Inspector
    Dim myObject As Object
    Dim myItem As mailItem

    Dim myDoc As Word.Document
    Dim mySelection As Word.Selection

    Set myInspector = ActiveInspector

    If myInspector Is Nothing Then
        MsgBox "No inspector. Open a mailitem and select some text."
        GoTo ExitRoutine
    End If

    If myInspector.EditorType <> olEditorWord Then
        'https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/oleditortype-enumeration-outlook
        '  olEditorWord / 4 / Microsoft Office Word editor
        Debug.Print "EditorType: " & myInspector.EditorType
        MsgBox "Editor is not Microsoft Office Word editor"
        GoTo ExitRoutine
    End If

    ' Probably not needed. EditorType should be enough
    'If myInspector.IsWordMail = False Then
    '    MsgBox "myInspector.IsWordMail = False"
    '    GoTo ExitRoutine
    'End If

    On Error Resume Next
    Set myObject = myInspector.currentItem
    On Error GoTo 0

    If myObject Is Nothing Then
        MsgBox "Open a mailitem and select some text."
        GoTo ExitRoutine
    End If

    If myObject.MessageClass = "IPM.Note" Then
        'Should be equivalent to If myObject.Class = olMail Then

        Set myItem = myObject

        Set myDoc = myInspector.WordEditor

        Set mySelection = myDoc.Application.Selection
        Debug.Print "Selected text is: " & mySelection
        MsgBox "Selected text is: " & vbCr & vbCr & mySelection

        mySelection.Font.name = "Consolas"
        mySelection.Paragraphs.Indent

    Else

        MsgBox "Not a mailitem. Open a mailitem and select some text."
        GoTo ExitRoutine

    End If

ExitRoutine:

    Set myInspector = Nothing
    Set myObject = Nothing
    Set myItem = Nothing

    Set myDoc = Nothing
    Set mySelection = Nothing

End Sub

Upvotes: 5

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

Looks like you are trying to mix the Word object model with Outlook. The Selection class in Outlook is not the same as the Selection class from the Word object model. Moreover, there is no such shortcuts in Outlook. You must retrieve it each time you need it.

The Outlook object model provides three main ways for working with item bodies:

  1. The Body property. A raw text.
  2. The HTMLBody property. The body is represented by the html markup.
  3. The Word object model. The WordEditor property of the Inspector class returns and instance of the Word Document class which represents the body.

You can read more about this in the Chapter 17: Working with Item Bodies article in MSDN. It describes all these properties in depth.

Upvotes: 1

Related Questions