Consulting

Results 1 to 5 of 5

Thread: To change paragraph styles in tables and add cell border, depending on the style used

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Newbie
    Joined
    Oct 2024
    Posts
    3
    Location

    Question To change paragraph styles in tables and add cell border, depending on the style used

    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 (I need the character style to stay).
    2. 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!
    Last edited by Aussiebear; 10-01-2024 at 02:18 PM. Reason: Indented code

  2. #2
    VBAX Newbie
    Joined
    Oct 2024
    Posts
    3
    Location
    Sorry, just realised I should have included multi-post links as below:
    stackoverflow.com
    answers.microsoft.com

  3. #3
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,442
    Location
    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.
    Last edited by macropod; 10-07-2024 at 02:24 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    VBAX Newbie
    Joined
    Oct 2024
    Posts
    3
    Location
    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.

  5. #5
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,442
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •