0

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Matt
  • 14,906
  • 27
  • 99
  • 149
  • Where did you get the Selection object? – Eugene Astafiev Jun 18 '18 at 09:28
  • @EugeneAstafiev I 'm not even sure if i need to use that as I will be manually highlighting text before i press the button – Matt Jun 18 '18 at 09:30
  • Note, Word provides a macro recorder which generates the required code for you. You just need to launch it and do the required actions manually. Then you will get the code. – Eugene Astafiev Jun 18 '18 at 09:32
  • @EugeneAstafiev That is what I did to get the above code that doesn't work – Matt Jun 18 '18 at 09:33
  • Well, you can't run it from Outlook. Outlook doesn't provide shortcuts like `Selection`. Instead, you need to retrieve the `Selection` object in the code. Just use the `WordEditor` property of the Inspector class to get an instance of the Document class from the Word object model (don't forget to add a COM reference). – Eugene Astafiev Jun 18 '18 at 09:36

2 Answers2

2

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
niton
  • 8,771
  • 21
  • 32
  • 52
  • Getting the error `Run-time error '91': Object variable or With block variable not set` – Matt Jun 19 '18 at 07:34
  • I would have to guess it is on `Set objDoc = ActiveInspector.WordEditor` meaning there is no open item. – niton Jun 19 '18 at 16:13
  • WHat do you mean by no open item? – Matt Jun 20 '18 at 07:59
  • With my setup I can recreate the error if there is no active editable mailitem. If you are following the process in the question there would be, and there is something else to look for. Would you confirm the line with the error. – niton Jun 20 '18 at 11:37
  • yes i had the reply window open and I had text highlighted. – Matt Jun 20 '18 at 11:39
  • Code with troubleshooting added. – niton Jun 20 '18 at 16:25
1

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45