2017-01-09 11 views
0

複数のテーブルを選択してテキストボックスオブジェクトに基づいて文字列を入力するMS WordのVBAコードが必要です。これを行うコードを書いたことがありますが、多数のテーブルを選択すると繰り返します。Word vba multiple table selection

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 

このコードを組み込む簡単な方法はありますか?

答えて

0

あなたがより簡単にそれを処理するために(私はいくつかのデータを取り出し、それはあなたのアイデアを与えるだろう)次のパターンを使用することができます。

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