none
Outlook vba: code to change message font to custom color RRS feed

  • Question

  • Hello 

    I frequently need to use custom colors in text when working in Outlook, PowerPoint, Word and Excel. To save having to set up custom colors each time, I've tried my hand at writing some code. I'm a newbie at VBA macros, but managed to write code in Word (which works great in that application) to change the color of selected text. But it doesn't work when I add the code to Outlook 2010. 

    I've been looking up Diane Poremsky's great advice on slipstick.com and have set the references in Outlook to recognize Word's object library.

    Can you help me? Here's the code for the customized shade of pink. I have the same code but for the other 6 colors needed. Just to be clear, I'm not after fixing the colors in stationery, I need to change the colors on certain chunks of text in any message.

    The error code is:

    run-time error 424 object required

    I am totally happy to be told it's my lack of experience. I'm guessing something vital is missing! 

    Many thanks

    Sub companypink()

    '

    ' companypink Macro

    '

    '

        Selection.Font.Color = 8724439

        Selection.EscapeKey

    End Sub




    • Edited by G00dbyegirl Friday, January 25, 2013 1:19 PM
    Friday, January 25, 2013 1:13 PM

Answers

  • Well, I got it working! Armed with your advice and some code I edited from slipstick.com, I came up with this. I also created buttons on the new message 'customize quick access toolbar' so I can change selected text with just one click. I created 3 buttons, one for each color. So the code is working in Outlook, I've got it working for Word (by recording a macro), next challenge is PowerPoint. 

    I need code to change just selected text. I can only find code that changes all the text on the slide. Any ideas? 

    Many thanks

    VBA to change selected text color on Outlook

       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

       

           With objSel

           ' Formatting code goes here

                .Font.Color = 10172245

                   End With

                End If

            End If

        End If

       

        Set objItem = Nothing

        Set objWord = Nothing

        Set objSel = Nothing

        Set objInsp = Nothing

    End Sub


    Saturday, January 26, 2013 4:55 PM

All replies

  • It really sounds like your question is specific to the Word Object Model, not Outlook.  We can certainly help you with your code to get a handle on the email that you want to modify, but you are not showing enough of your code.  Also, note which code line is generating the error.

    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Friday, January 25, 2013 3:06 PM
    Moderator
  • Hello Eric

    Thanks for your reply.

    I am a newbie at this but I definitely want to run the code in Outlook. My understanding is it's not possible to record macros in Outlook 2010, so I recorded the macro in Word 2010 and copied into VBA 'ThisOutlookSession' in Outlook. 

    I've pasted the full code in my original post. On run, the error is ' run-time error '424': Object required', no code line is generated.

    For experienced programmers this is probably laughably easy, so do feel free to go real basic with me.

    Many thanks for your help.

    Friday, January 25, 2013 5:00 PM
  • That code won't work in Outlook because you haven't got any code that is working on an email or working with the message body.  You have to use the Selection object to get a MailItem, then use MailItem.GetInspector to get an Inspector object, and work with the WordEditor proprety to get a Word Document object, then a Word Application object (Document.Application), then use can use Application.Selection.  You'll have to modify your code quite a bit, but I'm running out for the day and can't provide additional guidance at this time.  Here's your homework!

    Selection Object (Outlook):
    http://msdn.microsoft.com/en-us/library/ff860771(office.14).aspx

    MailItem.GetInspector Property (Outlook):
    http://msdn.microsoft.com/en-us/library/ff868098(office.14).aspx

    Inspector.WordEditor Property (Outlook):
    http://msdn.microsoft.com/en-us/library/ff868196(office.14).aspx


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Friday, January 25, 2013 5:12 PM
    Moderator
  • Ooooh, homework! Told you I'm a novice at this. Thanks and I'll have a go at this at the weekend and report back.
    Friday, January 25, 2013 9:22 PM
  • Well, I got it working! Armed with your advice and some code I edited from slipstick.com, I came up with this. I also created buttons on the new message 'customize quick access toolbar' so I can change selected text with just one click. I created 3 buttons, one for each color. So the code is working in Outlook, I've got it working for Word (by recording a macro), next challenge is PowerPoint. 

    I need code to change just selected text. I can only find code that changes all the text on the slide. Any ideas? 

    Many thanks

    VBA to change selected text color on Outlook

       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

       

           With objSel

           ' Formatting code goes here

                .Font.Color = 10172245

                   End With

                End If

            End If

        End If

       

        Set objItem = Nothing

        Set objWord = Nothing

        Set objSel = Nothing

        Set objInsp = Nothing

    End Sub


    Saturday, January 26, 2013 4:55 PM
  • Good job!  But sorry, I've never coded against the PowerPoint Object Model. You can ask here if you get stuck:

    General Office Development Forum:
    http://social.msdn.microsoft.com/Forums/en-US/officegeneral/threads


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Saturday, January 26, 2013 9:30 PM
    Moderator