如何在VBA Word中创建单独的表?

时间:2019-06-20 19:56:27

标签: vba ms-word

我正在尝试在VBA中创建两个单独的表

我检查了很多线程,但无法弄清楚。不断出现错误。我能够创建第一个表,或创建嵌套在第一个表中的第二个表,但是无法创建两个单独的表。

Dim objWord
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set oDoc = objWord.Documents.Add
Set oTable = oDoc.Tables.Add(oDoc.Range(0, 0), 6, 4)

oDoc.Paragraphs.Add
Set aRange = ActiveDocument.Paragraphs.Last.Range

Set oTable2 = oDoc.Tables.Add(oDoc.aRange, 5, 4)

oTable.Borders.Enable = True
oTable2.Borders.Enable = True

Set oTable2 = oDoc.Tables.Add(oDoc.aRange, 5, 4)行出现错误。我不确定该如何处理先前声明的aRange

1 个答案:

答案 0 :(得分:1)

尝试:

Dim wdApp As Object, wdDoc As Object, wdTbl As Object
Set wdApp = CreateObject("Word.Application")
With wdApp
  .Visible = True
  Set wdDoc = .Documents.Add
  With wdDoc
    Set wdTbl = .Tables.Add(.Range.Characters.Last, 6, 4)
    wdTbl.Borders.Enable = True
    .Range.InsertAfter vbCr
    Set wdTbl = .Tables.Add(.Range.Characters.Last, 5, 4)
    wdTbl.Borders.Enable = True
  End With
End With