字vba 多选table

Word vba multiple table selection

我需要一个 VBA 的 MS Word 代码,它 select 多个表格(我选择的)并输入基于文本框对象的字符串。我已经编写了执行此操作的代码,但如果我要 select 大量表,它会变得重复。

Private Sub Claim_Change()

  Dim j As Table
  Set j = ActiveDocument.Tables(2)
  Dim k As Table
  Set k = ActiveDocument.Tables(3)

'Input claim
j.Select
Selection.Delete

With j
.Cell(1, 1).Range.InsertAfter "Claim #: " & Claim
.Cell(1, 2).Range.Text = Format(Date, "MMMM DD, YYYY ")
.Columns.AutoFit
End With

k.Select
Selection.Delete

With k
.Cell(1, 1).Range.InsertAfter "Claim #: " & Claim
.Cell(1, 2).Range.Text = Format(Date, "MMMM DD, YYYY ")
.Columns.AutoFit

End With
Claim.Select

End Sub

有没有更简单的方法将这段代码放在一起?

您可以使用以下模式(我删除了一些数据,但它会给您思路)来更轻松地处理它:

Option Explicit

Private Sub Claim_Change()

Dim myTable As Table
Dim tables() As Variant
Dim tableCounter As Long

tables = Array(1, 2, 5, 21)

For tableCounter = LBound(tables) To UBound(tables)

   ActiveDocument.tables(tables(tableCounter)).Select
   Selection.Delete

Next tableCounter

End Sub