Shading in heading row of a table, finding RGB code with VBA

Hello to you all,

Happy new year!

I would like to know if there is a method of finding out the RGB code of the shading that is used in the heading row of a specific table in a document.
In theory the table is formatted with a table style that has the shading colour for the heading row specified in the style.

I would settle for getting the RGB from the style, but preferably would like to get it out of the properties of the selected table.
To complicate things, it also could be that the heading rows are not used (so not visible in the document) but are there in the table and/or style if you would show them.
Any ideas?

|

* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

Do you want to read the colors in an existing table or set the color?
Diane Poremsky [Outlook MVP]
Outlook Resources: https://www.slipstick.com
https://www.outlook-tips.net

** I don't work for Microsoft.**
** DO NOT CALL phone numbers received in email notifications.**

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi Diane,

I want to read the RGB in order to use it somewhere else.

There are 6 predefined table styles in this template which have shading added to the heading rows. I want to be able to give the user an option to add shading to a cell of their choice. The colour must correspond to the colour of the heading rows.

Of course I can write six procedures and allow the user to choose which they want, but I would prefer to create a general procedure which can be used on all tables.
So in order to do that I need to get the RGB of the heading row defined in the style of the table where the cursor is in.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

You can do something like this - assign the colors for foreground, background, etc to variables or just copy the colors.

With ActiveDocument
bgcolor = .Tables(1).Rows(1).Shading.BackgroundPatternColor
.Tables(2).Rows(1).Shading.BackgroundPatternColor = bgcolor
End With


With ActiveDocument
.Tables(1).Rows(1).Shading.BackgroundPatternColor = .Tables(2).Rows(1).Shading.BackgroundPatternColor
End With
Diane Poremsky [Outlook MVP]
Outlook Resources: https://www.slipstick.com
https://www.outlook-tips.net

** I don't work for Microsoft.**
** DO NOT CALL phone numbers received in email notifications.**

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi Diane,

Thanks.

This works only when the table is set to show with the heading rows. If you don't show those, the colour is not picked up (of course)
This is why I was wondering if there is no way to get into the table style properties for shading.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Are you using the predefined table grid styles? I don't know if you can pick up the style name, but you can set a table style if you know the name and apply it to the rows:
With Selection.Tables(1)
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With

Selection.Tables(1).Style = "Grid Table 4 - Accent 5"
Diane Poremsky [Outlook MVP]
Outlook Resources: https://www.slipstick.com
https://www.outlook-tips.net

** I don't work for Microsoft.**
** DO NOT CALL phone numbers received in email notifications.**

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Yes you can pick up the name.

I would prefer not to show the headingrows if the user did turn them off.

I need a button in the ribbon for shading alternative cells. 

For example if there are subheadings in the first column or in cells in another row. If the user decided not to show the heading rows (which is not abnormal if you consider the background of this question), who am I to turn them on again?

It surprises me that I could not find a way to drill into the properties of a table style. It can be done for paragraph styles, so why not for a table style?

I am looking for something like:
  Selection.Tables(1).Style.Table.Headingrows.Shading.BackgroundPatternColor

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Ok, one step closer. I tried to record the changing of a table style in order to see what code would come up. Well a lot ;-)
It can be done, but in a different way:

Sub ChangeShading()
Dim oStyle As Style
Dim oTable As Table
Dim bShowHeadings As Boolean
  If Selection.Tables.Count > 0 Then
    Set oTable = Selection.Tables(1)
  Else
    Exit Sub
  End If

  bShowHeadings = oTable.ApplyStyleHeadingRows

  Set oStyle = oTable.Style
  Selection.Cells(1).Shading.BackgroundPatternColor = oStyle.Table.Condition _
    (wdFirstRow).Shading.BackgroundPatternColor
  oTable.ApplyStyleHeadingRows = bShowHeadings

  Set oStyle = Nothing
  Set oTable = Nothing
End Sub

I needed to pick up the setting for showing headingrows, cause if I use this procedure, the heading rows are showed whatever the previous setting was. 

New problem arises. If I run the code, the coloring of the shading works fine. Even when the heading rows are not selected. However, if I run the code again in a different cell, the new cell is shaded, but the previous one looses it's shade. What am I doing wrong?

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Using Diane's code

With ActiveDocument
bgcolor = .Tables(1).Rows(1).Shading.BackgroundPatternColor
.Tables(2).Rows(1).Shading.BackgroundPatternColor = bgcolor
End With

to get the backgroundpatterncolor of the first row of a table does NOT require that the row be set as a HeadingRow.

If the following code is run when the selection is in a cell of a table, the shading of that cell will be set to the same backgroundpatterncolor as that of the first cell in the first row of the table

With Selection
    bgcolor = .Tables(1).Rows(1).Cells(1).Shading.BackgroundPatternColor
    .Cells(1).Shading.BackgroundPatternColor = bgcolor
End With

Before running the above code

After running the code

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi Doug,

Yes you are right. But this is not the problem.

I guess I didn't explain myself well enough. I'll try again.

  • User chooses one of six predefined table styles. Each style has a color defined for heading rows.
  • User chooses not to show the heading rows. Instead goes on editing in a further non shaded table (but with style applied)
  • User want to shade another cell. Let's say first column cell in third row
  • User runs code and ends up with no shading because there are no heading rows applied (showed)

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

The following will unhide the Heading Row if it is hidden so that the bgcolor can be set to the color of that row, then apply that colour to the selected cell and then hide the Heading Row if that is the way it was.

Dim blnheading As Boolean
With Selection
    If .Tables(1).Rows(1).HeadingFormat = False Then
        blnheading = False
        .Tables(1).Rows(1).HeadingFormat = True
    End If
    bgcolor = .Tables(1).Rows(1).Cells(1).Shading.BackgroundPatternColor
    .Cells(1).Shading.BackgroundPatternColor = bgcolor
    If blnheading = False Then
        .Tables(1).Rows(1).HeadingFormat = False
    End If
End With

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

 
 

Question Info


Last updated April 13, 2021 Views 407 Applies to: