none
If Background is Gray, Make Text Bold RRS feed

  • General discussion

  • I don't do much Word VBA stuff, so I'm a little out of my comfort zone here.  I though the code below would make the text inside of a tale bold, if the background was gray, but it actually does nothing at all.  The code runs, but doesn't actually do anything.

    Sub Change_All_Tables_Formating()
    Dim oTbl As Word.Table
    Dim oCell As Word.Cell
      For Each oTbl In ActiveDocument.Tables
      For Each oCell In oTbl.Range.Cells
            If oTbl.Rows(1).Shading.BackgroundPatternColor = wdColorGray25 Then
                oTbl.Rows(1).Range.Bold = True
            End If
      Next oCell
      Next oTbl
    End Sub

    What am I missing here?

    TIA!


    MY BOOK

    Thursday, September 1, 2016 8:10 PM

All replies

  • What happens if you click inside one of the cells with a gray background, then type the following in the Immediate window and press Enter?

    If it's really wdColorGray25, you should get 12632256


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, September 1, 2016 8:28 PM
  • If I type what into the immediate window???

    I just found this, which seems helpful.

    https://msdn.microsoft.com/en-us/library/bb237558%28v=office.12%29.aspx?f=255&MSPPError=-2147217396

    Even with that, I still can't get this working.  I gave up on the gray, as there are too many shades of gray to test this.  There must be 50 Shades of Gray!  LOL.  I tried green.  There are only a few shades of green.  I couldn't get that working either.


    MY BOOK

    Friday, September 2, 2016 2:21 PM
  • Sorry about that. I intended to write

    ? Selection.Shading.BackgroundPatternColor

    If you want to make text bold if the cell has any kind of shading, you could use

    Sub Change_All_Tables_Formating()
        Dim oTbl As Word.Table
        For Each oTbl In ActiveDocument.Tables
                Select Case oTbl.Rows(1).Shading.BackgroundPatternColor
                Case wdColorAutomatic, wdColorWhite
                    ' Skip
                Case Else
                    oTbl.Rows(1).Range.Bold = True
                End Select
        Next oTbl
    End Sub

    (Since you're looking at the first row only, there is no need to loop through all the cells)


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, September 2, 2016 2:52 PM
  • I actually want to look at all cells, not just the first row.  I modified your code a bit.  Still, nothing happens when I run it.  I set a breakpoint on the Bold line of the script, and it gets hit many times, but nothing gets bolded.  What am I missing here?

    Sub Change_All_Tables_Formating()
        Dim oTbl As Word.Table
        Dim ocell As Cell
        For Each oTbl In ActiveDocument.Tables
                oTbl.Select
                For Each ocell In oTbl.Range.Cells
                Select Case ocell.Shading.BackgroundPatternColor
                Case wdColorAutomatic, wdColorWhite
                    ' Skip
                Case Else
                    ocell.Range.Bold = True
                End Select
                Next ocell
        Next oTbl
    End Sub


    MY BOOK

    Friday, September 2, 2016 3:16 PM
  • Ok, I see how this works now.  You have to go to Design (tab) > Shading and shade the cell.

    I was doing this as Home (tab) > Text Highlight Color.

    Then, run the scrip below.

    Sub Change_All_Tables_Formating()
        Dim oTbl As Word.Table
        Dim ocell As Cell
        For Each oTbl In ActiveDocument.Tables
                oTbl.Select
                For Each ocell In oTbl.Range.Cells
                Select Case ocell.Shading.BackgroundPatternColor
                Case wdColorAutomatic, wdColorWhite
                    ' Skip
                Case Else
                    ocell.Range.Select
                    Selection.Font.Bold = wdToggle
                End Select
                Next ocell
        Next oTbl
    End Sub

    Thanks Hans!!


    MY BOOK

    Friday, September 2, 2016 3:36 PM