Search code examples
vbams-word

Inserting and Sizing a Table in Word Header


How do I consistently set an inserted table's column width to both edges of an document. The code I use works, but if the document margins vary then the table is off. Any guidance is appreciated. Below is the code I use:

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
    1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed
With Selection.Tables(1)
    If .Style <> "Table Grid" Then
        .Style = "Table Grid"
    End If
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = False
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = False
    .ApplyStyleRowBands = True
    .ApplyStyleColumnBands = False
    .Borders(wdBorderTop).LineStyle = wdLineStyleNone
    .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    .Rows.SetLeftIndent LeftIndent:=-62.1, RulerStyle:= _
    wdAdjustNone
    .Columns(1).SetWidth ColumnWidth:=597.6, RulerStyle:= _
    wdAdjustNone

Solution

    • First, it is necessary to determine the proportion of the table width (i.e., table width / header width), and then the position of the table can be determined.
    Option Explicit
    Sub InsertTableInHeader()
        Dim pageWidth As Single
        Dim leftMargin As Single
        Dim tableWidth As Single
        Dim tableLeftPosition As Single
        Const WIDTH_RATIO = 0.5  ' modify as needed
        ' Get the width of the header
        pageWidth = ActiveDocument.PageSetup.pageWidth
        ' Get the left and right margins
        leftMargin = ActiveDocument.PageSetup.leftMargin
        ' Calculate the position and width for inserting the table
        tableWidth = pageWidth * WIDTH_RATIO
        tableLeftPosition = (pageWidth - leftMargin * 2 - tableWidth) / 2
        ' Delete existing tables in the header
        Dim tblCount As Integer
        tblCount = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables.Count
        If tblCount > 0 Then
            Dim i As Integer
            For i = tblCount To 1 Step -1
                ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(i).Delete
            Next i
        End If
        ' Switch to header view and insert the table
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
        ' Set up the inserted table
        With Selection.Tables(1)
            If .Style <> "Table Grid" Then
                .Style = "Table Grid"
            End If
            .ApplyStyleHeadingRows = True
            .ApplyStyleLastRow = False
            .ApplyStyleFirstColumn = True
            .ApplyStyleLastColumn = False
            .ApplyStyleRowBands = True
            .ApplyStyleColumnBands = False
            ' *** For testing to show the table
            .Borders(wdBorderTop).LineStyle = wdLineStyleSingle
            .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
            .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
            .Borders(wdBorderRight).LineStyle = wdLineStyleSingle
            ' ***
            ' .Borders(wdBorderTop).LineStyle = wdLineStyleNone
            ' .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
            ' .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
            ' .Borders(wdBorderRight).LineStyle = wdLineStyleNone
            .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
            .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
            .Rows.SetLeftIndent LeftIndent:=tableLeftPosition, RulerStyle:=wdAdjustNone
            .Columns(1).SetWidth ColumnWidth:=tableWidth, RulerStyle:=wdAdjustNone
        End With
        ' Switch back to the main document view
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub
    

    enter image description here