Consulting

Results 1 to 6 of 6

Thread: How to change the font color of specific text within a Word table cell

  1. #1

    How to change the font color of specific text within a Word table cell

    I have hundreds of Word tables that have data cells formatted like:
    13.45 [56.34]

    I know how to highlight or change the font color for the entire cell, but what I want to be able to do is programmatically change ONLY the color of the numbers within the brackets. So the cell would look like:
    13.45 [56.34]

    Thanks in advance.

    Robert

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = "\[*\]"
        .MatchWildcards = True
        While .Execute
          If oRng.Information(wdWithInTable) Then
            With oRng
              .End = .End - 1
              .Start = .Start + 1
              .Font.ColorIndex = wdBlue
              .Collapse wdCollapseEnd
            End With
          End If
        Wend
      End With
    lbl_Exit:
      Exit Sub
      
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: http://www.msofficeforums.com/word-t...xt-within.html


    Please read VBA Express' policy onCross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    Greg, the code you provided works, but unfortunately it takes hours to run because the document is huge. Here's a snippet of code from the application. I think it would be much more efficient if I could just color the text as it's being inserted into the document at run time. The variable CELLVALUE is what gets wrapped in brackets and is what I want to color blue.

    Quote Originally Posted by rmatthews
    For Each TblCell In Tbl.Range.Cells
     With TblCell
      Set RNG = .Range
      With RNG
       If InStr(UCase$(.Text), CellID) > 0 Then
        If CellHasText = "N" Then
         .End = .Start + InStr(UCase$(.Text), CellID) - 1
         If CellValue <> "" Then
          .Text = .Text & vbCr & " [" & CellValue & "]"
          .Font.Hidden = False
          CellCounter = CellCounter + 1
         Else
             '  ... more code
          End if
        End if
        End if
     End With
     End With
    Next
    Quote Originally Posted by gmaxey View Post
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = "\[*\]"
        .MatchWildcards = True
        While .Execute
          If oRng.Information(wdWithInTable) Then
            With oRng
              .End = .End - 1
              .Start = .Start + 1
              .Font.ColorIndex = wdBlue
              .Collapse wdCollapseEnd
            End With
          End If
        Wend
      End With
    lbl_Exit:
      Exit Sub
      
    End Sub

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Seems that you could just add:

    .Font.ColorIndex = wdBlue

    to your existing macro after the .Font.Hidden = False line.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    As indicated in my reply to your cross-post (q.v.), if there isn't any content that might be affected outside tables, you could simply use:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Format = True
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWildcards = True
        .Replacement.Text = "^&"
        .Text = "\[[0-9.]{1,}\]"
        .Replacement.Font.ColorIndex = wdBlue
        .Execute Replace:=wdReplaceAll
        .Text = "[\[\]]"
        .Replacement.Font.ColorIndex = wdAuto
        .Execute Replace:=wdReplaceAll
      End With
    End With
    Application.ScreenUpdating = True
    End Sub
    If there is such content outside tables that you don't want to affect, you could use (the slower):
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Tbl As Table
    For Each Tbl In ActiveDocument.Range.Tables
      With Tbl
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Format = True
          .Forward = True
          .Wrap = wdFindStop
          .MatchWildcards = True
          .Replacement.Text = "^&"
          .Text = "\[[0-9.]{1,}\]"
          .Replacement.Font.ColorIndex = wdBlue
          .Execute Replace:=wdReplaceAll
          .Text = "[\[\]]"
          .Replacement.Font.ColorIndex = wdAuto
          .Execute Replace:=wdReplaceAll
        End With
      End With
    Next
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •