Macro to Format certain words in email message

Status
Not open for further replies.

McBanjo

Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
I am trying to build an Outlook Macro for use when composing an email message to reformat several specific words at once in the email body so they are in their correct branding colours depending on what word they are.

The code needs to find a word that starts with the string "xp" and then reformats the entire word to bold and then changes the colour of the xp to the correct brand's colour depending on what the word is.

e..g. any word with "xpstorm" or "xprafts" needs to be bolded and the xp colour changed to the corresponding brand's colour (xpstorm, xprafts)

I have this scenario working in Word 2013, and I have an example code set below that I have tried to modify to replicate this functionality in an Outlook email by referencing Microsoft Word 15.0 Object Library in the Visual Basic References Tool.

Any assistance someone can provide would be greatly appreciated. It's really gone above my head.

Code:
Sub XPBranding()
    Dim insp As Outlook.Inspector
    Dim myObject As Object
    Dim msg As Outlook.MailItem
    Dim myDoc As Word.Document
    Dim mySelection As Word.Selection
    Dim strItem As String
    Dim strGreeting As String
   
    'XPBranding section
    Dim StrTxt As String, Rng As Range
Dim tempFont As String
Dim tempColour As String
Dim tempBold As String
Dim StrTxt2 As String, Rng2 As Range
StrTxt = "xp"
'XPBranding finish
         
    Set insp = Application.ActiveInspector
    Set myObject = insp.CurrentItem
   
  
    'The active inspector is displaying a mail item.
    If myObject.MessageClass = "IPM.Note" And _
        insp.IsWordMail = True Then
        Set msg = insp.CurrentItem
        'Grab the body of the message using a Word Document object.
        Set myDoc = insp.WordEditor
        Set mySelection = myDoc.Application.Selection
        Set hed = msg.GetInspector.WordEditor
        Set appWord = hed.Application
        Set appRng = appWord.Selection
        With mySelection.Range
        With mySelection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "<" & StrTxt & "*>"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .MatchCase = False
    .Execute
        End With
          Do While .Find.Found
  If .Font.Name <> "Arial" Then
    tempFont = .Duplicate.Font.Name
    tempColour = .Duplicate.Font.Color
    tempBold = .Duplicate.Font.Bold
    With .Duplicate
      .Font.Size = .Font.Size + 2
      .Font.Name = "Zrnic"
      .Font.Bold = True
      If .Text <> "" Then
      Select Case Split(.Text, StrTxt)(1)
        Case "swmm"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(0, 122, 135)
        Case "swmmj"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(0, 122, 135)
        Case "swmmk"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(0, 122, 135)
        Case "swmmc"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(0, 122, 135)
        Case "storm"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(92, 127, 146)
        Case "2D"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(198, 96, 5)
        Case "2d"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(198, 96, 5)
        Case "rafts"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(102, 73, 117)
        Case "wspg"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(74, 170, 66)
        Case "culvert"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(0, 70, 173)
        Case "site3D"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(224, 170, 15)
        Case "paragon"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(71, 215, 172)
        Case "ertcare"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(79, 109, 94)
        Case "viewer"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(110, 178, 189)
        Case "drainage"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(35, 79, 51)
        Case "ratHGL"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(160, 0, 84)
        Case "rathgl"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(160, 0, 84)
      
        Case "dx"
          .Font.Name = tempFont
          .Font.Bold = tempBold
          .Font.Size = .Font.Size - 2
          .End = .Start + Len(StrTxt)
          .Font.Color = tempColour
        Case "x"
          .Font.Name = tempFont
          .Font.Bold = tempBold
          .Font.Size = .Font.Size - 2
          .End = .Start + Len(StrTxt)
          .Font.Color = tempColour
        Case "s"
          .Font.Name = tempFont
          .Font.Bold = tempBold
          .Font.Size = .Font.Size - 2
          .End = .Start + Len(StrTxt)
          .Font.Color = tempColour
            
    End Select
      End If
     
     
     
     
    End With
    End If
     mySelection.Find.Execute

  Loop
         End With
         End If
                
End Sub
 

McBanjo

Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
No errors. It seems to cycle through and select all the appropriate words in quick succession, but only selects, doesn't edit them.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
I'd walk through the code execution step for step by pressing f8, and see where it doesn't do what you expect. I haven't looked at the entire code; if there's a On Error Resume Next statement, remove it and see if you then get errors.

Not very necessary but for understanding the code it would be easier if you clean it up a little bit. For instance, you set two variables (mySelection and appRng) to the Word.Application.Selection object, which is confusing. Also, the latter seems to not be declared, which can cause logical errors as it then defaults to a Variant variable which behaves differently from the Selection object you want.
 

McBanjo

Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
I've had a good look and it seems to find the correct words, just it doesn't modify the formats.

Code:
Sub Branding()
    Dim objOL As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Dim objDoc As Word.Document
    Dim objSel As Word.selection
    Dim strStamp As String
    On Error Resume Next
    Set objOL = Application
    If objOL.ActiveInspector.EditorType = olEditorWord Then
        Set objDoc = objOL.ActiveInspector.WordEditor
        Set objNS = objOL.Session
        StrTxt = "xp"
        Set objSel = objDoc.Windows(1).selection
       
   With objSel.Range
   With objSel.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "<" & StrTxt & "*>"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .MatchCase = False
    .Execute
   End With
  
   Do While .Find.Found
  If .Font.Name <> "Arial" Then
    tempFont = .Duplicate.Font.Name
    tempColour = .Duplicate.Font.Color
    tempBold = .Duplicate.Font.Bold
    With .Duplicate
      .Font.Size = .Font.Size + 2
      .Font.Name = "Zrnic"
      .Font.Bold = True
      If .Text <> "" Then
      Select Case Split(.Text, StrTxt)(1)
        Case "swmm"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(0, 122, 135)
        Case "swmmj"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(0, 122, 135)
        Case "swmmk"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(0, 122, 135)
        Case "swmmc"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(0, 122, 135)
        Case "storm"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(92, 127, 146)
        Case "2D"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(198, 96, 5)
        Case "2d"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(198, 96, 5)
        Case "rafts"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(102, 73, 117)
        Case "wspg"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(74, 170, 66)
        Case "culvert"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(0, 70, 173)
        Case "site3D"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(224, 170, 15)
        Case "paragon"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(71, 215, 172)
        Case "ertcare"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(79, 109, 94)
        Case "viewer"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(110, 178, 189)
        Case "drainage"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(35, 79, 51)
        Case "ratHGL"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(160, 0, 84)
        Case "rathgl"
          .End = .Start + Len(StrTxt)
          .Font.Color = RGB(160, 0, 84)
      
        Case "dx"
          .Font.Name = tempFont
          .Font.Bold = tempBold
          .Font.Size = .Font.Size - 2
          .End = .Start + Len(StrTxt)
          .Font.Color = tempColour
        Case "x"
          .Font.Name = tempFont
          .Font.Bold = tempBold
          .Font.Size = .Font.Size - 2
          .End = .Start + Len(StrTxt)
          .Font.Color = tempColour
        Case "s"
          .Font.Name = tempFont
          .Font.Bold = tempBold
          .Font.Size = .Font.Size - 2
          .End = .Start + Len(StrTxt)
          .Font.Color = tempColour
            
    End Select
      End If
     
     
     
     
    End With
    End If
     objSel.Find.Execute
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
         End With
         End If
       
    Set objOL = Nothing
    Set objNS = Nothing
End Sub
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Why did you add On Error Resume Next?

The issue could be that you call .Range.Duplicate and change the properties of the copy instead of the original range. Another tip: Don't use nested With statements; I doubt anyone can see at a glance which objects the code is working on.
 
Status
Not open for further replies.
Top