You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA生成Word表格异常求助:粘连、重复显示及边框问题

Word VBA表格生成问题解决方案

原问题

  • 第一个表格与第二个表格粘连,无间距
  • 第二个表格内容跨页时,第一个表格在第二页重复显示
  • 已设置wrdTable1.Borders.Enable = False,但第一个表格仍显示网格线

修改后的完整代码

'    Set wrdTable1 = objDoc.Tables.Add(objDoc.Range, 20, 2)
Set wrdTable1 = objDoc.Tables.Add(Range:=objWord.Selection.Range, NumRows:=20, NumColumns:=2, _
     DefaulttableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed)

' 彻底关闭第一个表格的所有边框,解决网格线显示问题
wrdTable1.Borders.Enable = False
wrdTable1.Borders.InsideLineStyle = wdLineStyleNone
wrdTable1.Borders.OutsideLineStyle = wdLineStyleNone
' 关闭第一个表格的标题行重复,解决跨页重复问题
wrdTable1.Rows.HeadingFormat = False

With wrdTable1.Rows(1)
    .Cells(1).Range.Text = "Tele: @@@@"
    .Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
    .Cells(2).Range.Text = "!@@@@@@##@#"
    .Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With

wrdTable1.Rows(2).Cells(2).Range.Text = "PIN- 9@@@@@"
wrdTable1.Rows(3).Cells(2).Range.Text = "##########"
wrdTable1.Rows(20).Cells.Merge
wrdTable1.Rows(20).Cells(1).Range.Text = "2.   It is under ref :-"
wrdTable1.Rows(20).Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphJustify

wrdTable1.Range.InsertParagraphAfter
' 设置表格间的间距,解决粘连问题
wrdTable1.Range.Next.ParagraphFormat.SpaceAfter = 12

Set wrdTable2 = objDoc.Tables.Add(wrdTable1.Range.Next, ntotalRecords, ntotalColumns) 
Dim intRow As Integer
Dim intCol As Integer

With wrdTable2

    intCtr = 1: intRow = 1
    While (Sheets("SummaryDVBan").Cells(intCtr, 11).FormulaR1C1 <> "")

        For intCol = 1 To ntotalColumns

            .cell(intRow, intCol).Range.InsertAfter Sheets("SummaryDVBan").Cells(intCtr, intCol + 10).FormulaR1C1

        Next intCol

    intCtr = intCtr + 1
    intRow = intRow + 1
    Wend

    .Columns(1).SetWidth 40, wdAdjustFirstColumn
    .Columns(2).SetWidth 120, wdAdjustFirstColumn
    .Columns(3).SetWidth 60, wdAdjustFirstColumn
    .Columns(4).SetWidth 90, wdAdjustFirstColumn
    .Columns(5).SetWidth 65, wdAdjustFirstColumn
    .Columns(6).SetWidth 60, wdAdjustFirstColumn
    .Columns(7).SetWidth 90, wdAdjustFirstColumn

    .Style = "Table Grid"
    .Borders.Enable = True
    .Rows(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
    .Rows(1).Range.Bold = True
    .Rows(1).HeadingFormat = True
    .Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
    .Borders.InsideLineStyle = wdLineStyleSingle

End With
wrdTable2.Range.InsertParagraphAfter

Set wrdTable3 = objDoc.Tables.Add(wrdTable2.Range.Next, 7, 4) ' insert table 3
' 彻底关闭第三个表格的所有边框
wrdTable3.Borders.Enable = False
wrdTable3.Borders.InsideLineStyle = wdLineStyleNone
wrdTable3.Borders.OutsideLineStyle = wdLineStyleNone

w = wrdTable3.Rows(1).Cells(1).Width * 4
w1 = w * 0.26

For X = 1 To 7
   With wrdTable3.Rows(X)
        .Cells(1).Width = w1
        .Cells(2).Width = w1
        .Cells(3).Width = w1
        .Cells(4).Width = w1
        .Height = 16
    End With
Next X

wrdTable3.Rows(1).Cells.Merge
wrdTable3.Rows(1).Cells(1).Range.Text = "3.   The confirmation at the earliest pl."
wrdTable3.Rows(1).Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
wrdTable3.Rows(1).Cells(1).Range.ParagraphFormat.LineSpacingRule = wdLineSpaceAtLeast
wrdTable3.Rows(1).Cells(1).Range.ParagraphFormat.LineSpacing = 18



With objDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
    .Text = "REQUEST REMINDER"
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    .Font.Bold = True
    .Font.Name = "Arial"
    .Font.Size = 12
    .Font.Underline = wdUnderlineSingle
    .InsertParagraphAfter
    .ParagraphFormat.SpaceAfter = 12          ' twice the font size for 1 "blank line"
End With

End Sub

问题逐一解决说明

  1. 表格粘连无间距:在第一个表格插入段落之后,给新段落设置SpaceAfter = 12,确保两个表格之间有足够空白间距。
  2. 第一个表格跨页重复:给第一个表格添加wrdTable1.Rows.HeadingFormat = False,明确关闭标题行重复功能,避免表格被当作标题行在新页重复显示。
  3. 表格仍显示网格线:仅设置Borders.Enable = False可能被表格样式覆盖,因此额外添加InsideLineStyle和OutsideLineStyle为wdLineStyleNone,彻底清除所有边框线,第一个和第三个表格都需要此设置。

内容的提问来源于stack exchange,提问作者veer salaria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 05:42:26