Share via

To change paragraph styles in tables and add cell border, depending on the style used (VBA Word for Mac)

Anonymous
2024-09-23T17:13:11+00:00

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:

  1. If the text has a character style enabled, the macro skips that text altogether.
  2. The macro left aligns everything that it changes 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!

Microsoft 365 and Office | Word | For business | MacOS

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2024-09-25T08:32:31+00:00

    Thanks for your quick response Sophia! Yes, I've tried Stack Overflow (https://stackoverflow.com/q/78940437/27166708), but had nothing useful back yet and can't find what I need in any books or online so far. Hopefully I'll get a response on this site.

    tabletim

    0 comments No comments
  2. Anonymous
    2024-09-24T07:22:50+00:00

    Hello tabletim,

    Good day!!

    Since we mainly focus on general and built-in feature queries in Word application, I sincerely recommend you refer to this article: Office VBA support and feedback | Microsoft Docs to go to Stack Overflow by using the VBA tag, along with any other relevant tags as there are also many experienced engineers and experts in the forums there. They will focus on the specific situation and provide specific suggestions for you.

    In the meanwhile, I will also keep this thread open so that community members and Word MVPs can share suggestions and insights here.

    Thanks for your cooperation and understanding! Hope you have a nice day!

    Best regards,

    Sophia

    0 comments No comments