PDA

View Full Version : [SOLVED:] To change paragraph styles in tables and add cell border, depending on the style used



tabletim
10-01-2024, 07:45 AM
I have documents that use paragraph style borders in tables instead of cell borders, and need a macro which will change them in each case to cell borders. After some online research I've come up with the following code, which works to some extent, but there are two problems:


If the text has a character style enabled, the macro skips that text altogether (I need the character style to stay).
The macro left aligns everything that it does change, but I need it to ignore alignment and keep it as it was.

Here is the code I am using:


Sub AutoFormatTables2024()
'Removes paragraph rules and replaces them with cell borders
Dim oCell As Cell
Dim oTable As Table
For Each oTable In ActiveDocument.Tables
For Each oCell In oTable.Range.Cells
Select Case True
Case oCell.Range.Style = ActiveDocument.Styles("_table figshead thin")
Options.DefaultBorderLineWidth = wdLineWidth050pt
oCell.Range.Style = ActiveDocument.Styles("_table figshead")
With oCell.Borders(wdBorderBottom)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Case oCell.Range.Style = ActiveDocument.Styles("_table figs thin")
Options.DefaultBorderLineWidth = wdLineWidth050pt
oCell.Range.Style = ActiveDocument.Styles("_table figs")
With oCell.Borders(wdBorderBottom)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Case oCell.Range.Style = ActiveDocument.Styles("_table figs thick")
Options.DefaultBorderLineWidth = wdLineWidth150pt
oCell.Range.Style = ActiveDocument.Styles("_table figs")
With oCell.Borders(wdBorderBottom)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
End Select
Next oCell
Next oTable
MsgBox "All done!"
End Sub

Hope someone can help!

tabletim
10-03-2024, 09:26 AM
:bug: Sorry, just realised I should have included multi-post links as below:
stackoverflow.com (https://answers.microsoft.com/en-us/msoffice/forum/all/to-change-paragraph-styles-in-tables-and-add-cell/0d5df720-cf92-4acf-99a6-e3260df056cf?messageId=e9e17419-3175-49a9-bb2a-7cf7ee708be6)
answers.microsoft.com (https://answers.microsoft.com/en-us/msoffice/forum/all/to-change-paragraph-styles-in-tables-and-add-cell/0d5df720-cf92-4acf-99a6-e3260df056cf?messageId=e9e17419-3175-49a9-bb2a-7cf7ee708be6)

macropod
10-03-2024, 01:10 PM
For problem 1, you might try changing:

Case oCell.Range.Style
to

Case oCell.Range.Paragraphs.First.Style


For problem 2, make sure the new Styles have the correct alignment.

tabletim
10-07-2024, 02:12 AM
Thanks again, Paul! This does work, in that it now includes paragraphs with a character style enabled, but now it doesn't add the cell borders. Do you have any idea why this would happen? I have found a separate macro which aligns all tables correctly (all columns right aligned except the first), which can be run after this one. :yes

macropod
10-07-2024, 02:23 PM
The change to the Case statements I suggested can't affect the application of the cell borders. It seems you changed more than what I suggested.