vbaoutlookoutlook-2016

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


Solution

  • 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