Search code examples
vbams-wordborder

How can I add internal borders to a Word table after encountering an error?


For a Word 97-02 doc table, I've modified the following macro from http://www.thedoctools.com/downloads/basTableBorder.shtml for my purposes so that it now looks like this:

Sub ApplyUniformBordersToAllTables()
    Dim oTable As Table
    Dim oBorderStyle As WdLineStyle
    Dim oBorderWidth As WdLineWidth
    Dim oBorderColor As WdColor
    Dim oarray As Variant
    Dim n As Long
    Dim i As Long
    '=========================
    'Change the values below to the desired style, width and color
    oBorderStyle = wdLineStyleSingle
    oBorderWidth = wdLineWidth050pt
    oBorderColor = wdColorAutomatic
    '=========================
    'Define array with the borders to be changed
    oarray = Array(wdBorderTop, _
        wdBorderLeft, _
        wdBorderBottom, _
        wdBorderRight, _
        wdBorderHorizontal, _
        wdBorderVertical)
    For Each oTable In ActiveDocument.Tables
        'Count tables - used in message
        n = n + 1
        With oTable
            For i = LBound(oarray) To UBound(oarray)
                'Skip if only one row and wdBorderHorizontal
                If .Rows.Count = 1 And oarray(i) = wdBorderHorizontal Then GoTo Skip
                'Skip if only one column and wdBorderVertical
                If .Columns.Count = 1 And oarray(i) = wdBorderVertical Then GoTo Skip
                With .Borders(oarray(i))
                    .LineStyle = oBorderStyle
                    .LineWidth = oBorderWidth
                    .Color = oBorderColor
                End With
            Next i
        End With
Skip:
    Next oTable
End Sub

The macro was modified to prevent errors when it encounters tables with only one row or one column, however, it still doesn't add the internal borders after encountering the problem.

Can anyone offer any suggestions?


Solution

  • I'm not sure where your error is occuring, but I am curious why there is so much checking to make sure a single cell table is skipped since it will throw an error. There's a good way to avoid errors: Place "On Error Resume Next" in your code. This is a classic example of where you might want to do this. When it tries to do something it can't because of the size, it'll keep going, ensuring all tables, even single cells ones, are formatted to your liking.

    Some points I found unnessesary:

    • No need to keep track of table count with "n" since you can just use the tables.count property
    • No need to check bounds if the array is declared inside the cound (you know the bounds!)
    • No reason to declare the border const types as variables.

    Here is a more cleaned up version of the code for your reference (plus I made some changes based on personal preference, particularly variable naming; I feel there is no need for Hungarian notation with VBA, especially considering code should be short enough for you to look up and see the variable declarations if need be).

    Sub ApplyUniformBordersToAllTables()
    
    Dim table As table
    Dim borderType As Variant
    Dim i As Long
    
    borderType = Array(wdBorderTop, wdBorderLeft, wdBorderBottom, _
                       wdBorderRight, wdBorderHorizontal, wdBorderVertical)
    
    On Error Resume Next
    For Each table In ActiveDocument.Tables
        With table
            For i = 0 To 5
                With .borders(borderType(i))
                    .LineStyle = wdLineStyleSingle
                    .LineWidth = wdLineWidth050pt
                    .Color = wdColorAutomatic
                End With
            Next
        End With
    Next
    
    MsgBox ActiveDocument.Tables.Count & " tables formatted."
    
    End Sub