Consulting

Results 1 to 3 of 3

Thread: Macro that changes the style of all tables in document

  1. #1
    VBAX Newbie
    Joined
    Feb 2022
    Posts
    1
    Location

    Macro that changes the style of all tables in document

    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?

  2. #2
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    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

  3. #3
    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

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
  •