在Word标题中插入表格并调整表格大小

问题描述 投票:0回答:1

如何将插入表格的列宽一致设置为文档的两侧。我使用的代码有效,但如果文档边距发生变化,那么表格就会关闭。任何指导表示赞赏。以下是我使用的代码:

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
vba ms-word
1个回答
0
投票
  • 首先需要确定表格宽度的比例(即表格宽度/表头宽度),然后才能确定表格的位置。
Option Explicit
Sub InsertTableInHeader()
    Dim pageWidth As Single
    Dim leftMargin As Single
    Dim rightMargin 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
    rightMargin = ActiveDocument.PageSetup.rightMargin
    ' 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

© www.soinside.com 2019 - 2024. All rights reserved.