Search code examples
vbams-publisher

How can I set the background colour of the selected cell in publisher


I'm trying to create a macro that sets the font colour of text in a cell to white and the cell background to black using VBA in Publisher.

So far I have managed to set up the font colour to change but I'm really struggling with the background - I can't find the right value to change.

Here's what I have so far:

Sub set_to_clue()

Selection.TextRange.Font.Color.RGB = RGB(255, 255, 255)
Selection.TextRange.Font.Fill.BackColor.RGB = RGB(0, 0, 0)

End Sub

Progress With a bit of further trial and error I have worked out how to get cell backgrounds to change, however currently I can only do it by specifying an item number for the CellRange. This means that the cell that changes colour is hard coded rather than the selected one. How can I calculate the item number?

Sub set_to_clue()

Selection.TextRange.Font.Color.RGB = RGB(255, 255, 255)
Selection.TableCellRange.Item(10).Fill.ForeColor.RGB = RGB(0, 255, 0)

End Sub

Solution

  • I now have a working version, though I am sure it is not the correct or most elegant way to achieve the goal.

    It also currently only works if the cell itself is entirely highlighted rather than just the text within it or just the cursor being in the cell. I may work to improve this later.

    Working code in Publisher 2016:

    Sub invert_square()
    
    For Each square In Selection.TableCellRange
        If square.Selected = True Then
            square.Fill.ForeColor.RGB = RGB(0, 0, 0)
            square.TextRange.Font.Color.RGB = RGB(255, 255, 255)
            Exit For
        End If
        Next
    
    End Sub