在 Word VBA 中合并 header 中表格的单元格

Merge cells of tables in the header in Word VBA

我想合并 Word 文档 header 中第二个 table 的两个单元格。我创建了下面的脚本,但它有一个 run-time 错误:'5491'。collection 的请求成员不存在。

此行发生错误“With xTable(2)”

Sub mergercells()
    
    Set xTable = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables

 With xTable(2)
 .Cell(Row:=3, Column:=2).Merge _
 MergeTo:=.Cell(Row:=3, Column:=1)
 .Borders.Enable = False
 End With

End Sub

谢谢,

您只需要:

Sub MergeCells()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(2)
  .Cell(Row:=3, Column:=2).Merge MergeTo:=.Cell(Row:=3, Column:=1)
  .Borders.Enable = False
End With
End Sub