none
How do I change one of several colors programatically in a large Word document? RRS feed

  • Question

  • I have a large Word document that has many paragraphs and cells within various tables containing a specific background color that I want to change to white (or any other color for that matter), but I want other colors to remain unchanged.  Here is my macro script, which I cannot make any headway.  Thanks in advance for your help!

    Sub ChangeColor()
    '
    ' ChangeColor Macro
    '
    '
    Const wdColorBluish = 52479
    Const wdColorWhite = 16777215

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True

    Set objDoc = objWord.Documents.Open("C:\Software\Change color\Test document.doc")
    Set objSelection = objWord.Selection

    Dim rngTemp As Range

    For i = 1 To 102400
        Set rngTemp = ActiveDocument.Paragraphs(i).Range
        If rngTemp.Shading.BackgroundPatternColorIndex = wdColorBluish Then
            rngTemp.Shading.BackgroundPatternColorIndex = wdColorWhite
        End If
    Next

    End Sub

     


    Monday, April 11, 2011 8:45 PM

Answers

  • Hi Tony,

    FYI, after testing your code on my Office 2010 machine, I found it didn't matter if "as Paragraph" was there or not.

    Also I agree, if Michael's document has text shaded versus paragraphs that he'll have to use your alternate code. However, if he also has paragraphs shaded, he'll have to make a pass with the paragraph loop routine. And if he has table cells shaded then another routine to take care of those.

    Finally, I noticed in my test that the color number 52479 is in the "orange" family of colors and not blue.  So .... that made me wonder is he dealing with a Highligt and not a Shading? 


    Regards
    Wednesday, April 13, 2011 2:26 PM
  • Hi Rich,

    With the last set of code I posted, you don't need separate loops for paragraphs or tables. It fixes them all in one pass.

    And yes, I thought of hightlight instead of shading too, but I decided to take 'shading' at face value for the time being. Besides, hightlighting only supports 16 colorindex values, not 64K of color values.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Wednesday, April 13, 2011 2:57 PM

All replies

  • Try

    Sub ChangeColor()
        Dim objWord As Object
        Dim objDoc As Object
        Dim objSelection As Object
        Dim rngTemp As Object
        Const wdColorBluish = 52479
        Const wdColorWhite = 16777215

        Set objWord = CreateObject("Word.Application")
        objWord.Visible = True

        Set objDoc = objWord.Documents.Open("C:\Software\Change color\Test document.doc")
        With objDoc.Content.Find
            .Text = ""
            .ClearFormatting
            .ParagraphFormat.Shading.BackgroundPatternColor = wdColorBluish
            .Replacement.Text = ""
            .Replacement.ClearFormatting
            .Replacement.ParagraphFormat.Shading.BackgroundPatternColor = wdColorWhite
            .Execute Replace:=wdReplaceAll
        End With
    End Sub


    Regards, Hans Vogelaar
    Monday, April 11, 2011 9:53 PM
  • Hi Michael,

    The issue is that you are using “ColorIndex” but giving it the “Color” value. ColorIndex is a simple number such as 0 for nothing or 10 for blue.

    Here’s a simple example that scans all paragraphs in a document and changes the color from Blue to nothing.

        Dim para As Paragraph

          For Each para In ActiveDocument.Paragraphs

            If para.Shading.BackgroundPatternColor = 12611584 Then

                para.Shading.BackgroundPatternColorIndex = 0

            End If

        Next

    Hope this helps


    Regards
    Monday, April 11, 2011 10:10 PM
  • Hans,

    Thank you for your tips, but it didn't change any background colors in my test document.  I'll keep trying.

    Mike

    Tuesday, April 12, 2011 12:04 AM
  • Hi, Rich,

    Thank you for your hints.  But, I didn't know how to incorporate ActiveDocument into my macro.  Any further help will be greatly appreciated.

    Mike

    Tuesday, April 12, 2011 12:05 AM
  • Hi Mike,

    Try:
    Sub ChangeColor()
    Dim objWord As Object
    Dim objDoc As Object
    Dim objPara
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Open("C:\Software\Change color\Test document.doc")
    For Each objPara In objDoc.Paragraphs
      If objPara.Shading.BackgroundPatternColor = 12611584 Then
        objPara.Shading.BackgroundPatternColorIndex = 0
      End If
    Next
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, April 12, 2011 12:32 AM
  • Hi, Paul,

    That didn't work either.   Here is a snippet of text from my Word document, hope it shows up:

    Figure 1 shows the basic software components and input/output files of the software system...

     

    should be run to determine whether any code needs to be re-generated...

     

     

    Color

    Description

     

    row 1

     

    Old changes

     

    Handled by ...


    Tuesday, April 12, 2011 12:44 AM
  • The table with blue colored cell didn't come thru.  One of the table cells has the same background color as the phrases above ...

    thanks!

    Mike

    Tuesday, April 12, 2011 12:45 AM
  • Hi Michael,

     

    Use your original color code of 52479.  The other number that I provided was meant just as an example based on a test that I had run.

     

    Paul provided you the mechanism to adjust for the ActiveDocument statement that I had used.   I was using direct VBA in a Normal template to test and check my response to you.


    Regards
    Tuesday, April 12, 2011 1:22 AM
  • Hi Mike,

    I simply took Rich's code and adapted it to do away with the 'ActiveDocument' reference. I'm not sure where Rich got the '12611584' value from - you may need to use your original value (52479)


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, April 12, 2011 1:23 AM
  • Thank you, Rich,

    I am also using direct VBA in a Normal template to test this out.  This modified version below doesn't work, but it doesn't crash like my first attempt:

    Sub ChangeColor()
    '
    ' ChangeColor Macro
    '
    '
        Dim objWord As Object
        Dim objDoc As Object
        Dim objSelection As Object
        Dim rngTemp As Object
        Const wdColorBluish = 52479
        Const wdColorWhite = 16777215

        Set objWord = CreateObject("Word.Application")
        objWord.Visible = True

        Set objDoc = objWord.Documents.Open("C:\Software\Change color\Test Document.doc")
        With objDoc.Content.Find
            .Text = ""
            .ClearFormatting
            .ParagraphFormat.Shading.BackgroundPatternColor = wdColorBluish
            .Replacement.Text = ""
            .Replacement.ClearFormatting
            .Replacement.ParagraphFormat.Shading.BackgroundPatternColor = wdColorWhite
            .Execute Replace:=wdReplaceAll
        End With

    End Sub

    Tuesday, April 12, 2011 4:25 PM
  • Thank you, Paul, I did use my "blue" value as I included in my response to Paul above.  I am still not having success.  Still trying ...

    regards,

    Mike


    I've tried both versions of replacement lines (one is commented out) and still doesn't work:

    Sub ChangeColor2()
    '
    ' ChangeColor2 Macro
    '
    '
        Dim objWord As Object
        Dim objDoc As Object
        Dim objPara
        Set objWord = CreateObject("Word.Application")
        objWord.Visible = True
        Set objDoc = objWord.Documents.Open("C:\Software\Change color\Test document.doc")
        For Each objPara In objDoc.Paragraphs
          If objPara.Shading.BackgroundPatternColor = 52479 Then
    '        objPara.Shading.BackgroundPatternColorIndex = 0
            objPara.Shading.BackgroundPatternColor = 16777215
          End If
        Next
    End Sub

     

    Tuesday, April 12, 2011 4:27 PM
  • Hi Michael,

    Change this line:

    If rngTemp.Shading.BackgroundPatternColorIndex = wdColorBluish Then

     

    to

    If rngTemp.Shading.BackgroundPatternColor = wdColorBluish Then

     

     


    Regards
    Tuesday, April 12, 2011 5:33 PM
  • Hi Michael,

    Have you verified that the color codes you are using are the ones in the document?

    Also, in the above code objPara needs to be defined as a Paragraph.

    Dim objPara as Paragraph


    Regards
    Tuesday, April 12, 2011 6:03 PM
  • Hi Rich,

    If you do that, the code won't run.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, April 12, 2011 10:33 PM
  • Hi Michael,

    From a review of your post with the highlighted text, it would seem that the shading is not necessarily applied to whole paragraphs but is sometimes only applied to certain words. Is that so? Because if it is, a different approach will be needed.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, April 12, 2011 10:56 PM
  • On the assumption that text other than whole paragraphs may be shaded, try:

    Sub ChangeColor()
    Dim objWord As Object, objDoc As Object, objTxt
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Open("C:\Software\Change color\Test document.doc")
    For Each objTxt In objDoc.Range.Words
      On Error Resume Next
      If objTxt.Shading.BackgroundPatternColor = 52479 Then
        objTxt.Shading.BackgroundPatternColor = -16777216
      End If
    Next
    End Sub

    Note: This will be considerably slower than testing whole paraghraphs. The '-16777216' is Word's auto color setting. You might also want to test the selection's colour - on my system, 52479 is a mustard colour, not blue!


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, April 12, 2011 11:54 PM
  • Hi Tony,

    FYI, after testing your code on my Office 2010 machine, I found it didn't matter if "as Paragraph" was there or not.

    Also I agree, if Michael's document has text shaded versus paragraphs that he'll have to use your alternate code. However, if he also has paragraphs shaded, he'll have to make a pass with the paragraph loop routine. And if he has table cells shaded then another routine to take care of those.

    Finally, I noticed in my test that the color number 52479 is in the "orange" family of colors and not blue.  So .... that made me wonder is he dealing with a Highligt and not a Shading? 


    Regards
    Wednesday, April 13, 2011 2:26 PM
  • Hi Rich,

    With the last set of code I posted, you don't need separate loops for paragraphs or tables. It fixes them all in one pass.

    And yes, I thought of hightlight instead of shading too, but I decided to take 'shading' at face value for the time being. Besides, hightlighting only supports 16 colorindex values, not 64K of color values.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Wednesday, April 13, 2011 2:57 PM