PDA

View Full Version : Macro that changes the style of all tables in document



Amber
02-21-2022, 07:16 AM
I am trying to make a macro that changes the style of all tables in a document. The style it needs to be in has no borders except for the bottom border of the header row.

I've managed to get it to remove all borders, but I can't seem to figure out how to get that one border back in.

This is what I have now:


Sub tablestyle()For Each tbl In ActiveDocument.Tables


'only do this for a table if the first paragraph in the first table cell has the style "Table Heading"
If (tbl.Cell(1, 1).Range.Paragraphs(1).Style.NameLocal = "Table Heading") Then
tbl.Select

'set border lines to no border
Options.DefaultBorderLineWidth = wdLineWidth150pt
Options.DefaultBorderColor = wdColorBlue
With Selection.Borders(wdBorderTop)
.LineStyle = noborder


End With
With Selection.Borders(wdBorderLeft)
.LineStyle = noborder


End With
With Selection.Borders(wdBorderBottom)
.LineStyle = noborder
End With
With Selection.Borders(wdBorderRight)
.LineStyle = noborder


End With

' Set inside lines to no border
Options.DefaultBorderLineWidth = wdLineWidth075pt
Options.DefaultBorderColor = wdColorRed
With Selection.Borders(wdBorderHorizontal)
.LineStyle = noborder


End With
With Selection.Borders(wdBorderVertical)
.LineStyle = noborder


End With
End If
Next
End Sub


Let me add that this is my first time working with VBA, and my understanding of it is not that great yet.

Can anyone give me some pointers on how to make this work?

Dave
02-22-2022, 10:08 AM
Hi Amber and welcome to this forum. I'm not certain, but I think you can just code the format for each table. Something like this...

With tbl
.AutoFormat Format:=16, applyborders:=True
.AutoFitBehavior (1)
End With
You can mess with the number (16) until you find the format you want. The 16 is a constant which represents different table types. Perhaps others may have a better solution. Good luck. Dave

Souriane
02-22-2022, 07:46 PM
Hi!

Add this code add the end of your macro, just before the line "Next".



With tbl.Rows(1).Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
End With


Souriane