ActiveDocument.Tables(k).Range.Cells(j).Shading.BackgroundPatternColor returns -1 RRS feed

  • Question

  • After a long search, I am coming to you asking kindly for your help.

    I have a Word 2007 doc with a lot of tables. Each table has certain cells filled with 2 custom colors. I created a macro which takes for input 3 values to feed the RGB function, creating one of the colors, matches it against each of the colors, and changes the match with a new color.

    I used an If statement that compares the color of the filled cell with the color returned by the RGB function, feeding the function with the input given by the user.

    If ActiveDocument.Tables(k).Range.Cells(j).Shading.BackgroundPatternColor = RGB(inputRed, inputGreen, inputBlue) Then
    end if

    As I was looking through the document to see the results, I noticed that a few cells from 3 tables were left with the old color, so apparently the macro could not recognize it.

    In Word 2007 I selected the cell that was supposed to have its color changed. I went to Home -> Shading -> More colors -> Custom and saw the 3 values that matched perfectly with the 3 values given as input by the user.

    After 30 minutes of staring at the monitor, I thought about going to the doctor to have my eyes examined. :)

    Before doing that though, I started to debug the app. on each of the cells that were not changed. Upon examining this line:
    I saw that it returned -1.

    I am thinking -1 is the equivalent of null or nil, meaning that either the cell is not filled, but if so, why can I see the RGB values? Or perhaps the system can't read the RGB values, but who is this system exactly?

    Do you have a link where the Shading.BackgroundPatternColor method is well explained?

    Thank you very much for all your help!

    Monday, October 31, 2011 1:31 PM


  • Have you solved this?

    Shading.BackgroundPatternColor is a property, not a method. When you say it 'returned -1', do you you mean you were unable to access it? Or, are you talking about the RGB function?

    Shading.BackgroundPatternColor uses the WdColor enumeration, and I do not think it can be set to -1 (although I may be wrong).

    • Edited by JosephFox Saturday, November 5, 2011 6:23 PM English spelling 'colour' changed
    • Marked as answer by Calvin_GaoModerator Monday, November 7, 2011 8:59 AM
    Saturday, November 5, 2011 6:22 PM