Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the selected text from a WordEditor Object and change it's color?

I'm trying to use the WordEditor object to modify the color of the selected text (Outlook VBA) but i'm unable to find documentation or examples on how to do it. Any ideas?

I don't want to use the HTML editor, i need a solution for WordEditor.

I tried debuging the code and using OutlookSpy, but everytime i go into WordEditor.Content my outlook freezes and restarts :(.

Using Outlook 2010 on Windows 7

like image 941
RonK Avatar asked Sep 18 '25 15:09

RonK


1 Answers

OK - I found something that works. Ugly, but works:

Sub EmphesizeSelectedText(color As Long)
    Dim msg As Outlook.MailItem
    Dim insp As Outlook.Inspector

    Set insp = Application.ActiveInspector
    If insp.CurrentItem.Class = olMail Then
        Set msg = insp.CurrentItem
        If insp.EditorType = olEditorWord Then

            Set document = msg.GetInspector.WordEditor
            Set rng = document.Application.Selection

            With rng.font
                .Bold = True
                .color = color
            End With
        End If
    End If
    Set insp = Nothing
    Set rng = Nothing
    Set hed = Nothing
    Set msg = Nothing
End Sub

Eventually I found a reference that WordEditor returns a Document object. From there it was 2 hrs of going over MSDN's very slow web-help to find out that to get the selected text i needed to go up one level to the Application. Important note - changing rng.Style.Font did not do what i wanted it to do, it changed the entire document, when i started using the with rng.font my problem was solved (Thanks to Excel's marco recording abilities for showing me the correct syntax)

like image 162
RonK Avatar answered Sep 21 '25 09:09

RonK