Question

I am used in Excel and am not very experienced with Word VBA. I don't know how to get at a selection from a word table. I have tried something like the below but I don't know how to specify the selection from the currently selected table.

Sub Fill_Bold()
    For Each Cell In Selection.Cells
        If Cell.Font.Bold = True Then
            Cell.Shading.BackgroundPatternColor = wdColorRed
        End If
    Next Cell
End Sub
Was it helpful?

Solution

It's a bit unclear what you are asking about but I prepared the answer referring to the title of your question. Assumptions:

  1. your selection is within any table
  2. you want to search for any cell which is bold
  3. fill it with red color.

Here is your code improved where required:

Sub Fill_Bold()
Dim myCell
For Each myCell In Selection.Tables(1).Range.Cells

    If myCell.Range.Font.Bold = True Then

        myCell.Shading.BackgroundPatternColor = wdColorRed

    End If
Next myCell

End Sub

Important! You can't use cell variable name as cell is an object in Word, in fact both cell and cells are object/collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top