0

何が間違っているのか知りたいのですが...

3 つのテーブルを含む Word 文書を (Word 2010 で) 開いています。VBA で基本的なテーブル抽出をテストしたかったので、http://msdn.microsoft.com/en-us/library/office/aa537149(v=office.11 ​​).aspx の指示に従いました。

    Sub ExtractTableData()
    Dim doc As Word.Document
    Dim tbl As Word.Table
    Dim rng As Word.Range
    Dim sData As String
    Dim aData1() As String
    Dim aData2() As String
    Dim aDataAll() As String
    Dim nrRecs As Long
    Dim nrFields As Long
    Dim lRecs As Long
    Dim lFields As Long

    Set doc = ActiveDocument
    Set tbl = doc.Tables(1)
    Set rng = tbl.ConvertToText(Separator:=vbTab, _
        NestedTables:=False)
    ' Pick up the delimited text into and put it into a string variable.
    sData = rng.Text
    ' Restore the original table.
    doc.Undo
    ' Strip off last paragraph mark.
    sData = Mid(sData, 1, Len(sData) - 1)
    ' Break up each table row into an array element.
    aData1() = Split(sData, vbCr)
    nrRecs = UBound(aData1())
    ' The messagebox below is for debugging purposes and tells you
    ' how many rows are in the table.  It is commented out but can
    ' be used simply by uncommenting it.
    'MsgBox "The table contained " & nrRecs + 1 & " rows"
    'Process each row to break down the field information
    'into another array.
    For lRecs = LBound(aData1()) To nrRecs
        aData2() = Split(aData1(lRecs), vbTab)
        ' We need to do this only once!
        If lRecs = LBound(aData1()) Then
            nrFields = UBound(aData2())
            ReDim Preserve aDataAll(nrRecs, nrFields)
        End If
        ' Now bring the row and field information together
        ' in a single, two-dimensional array.
        For lFields = LBound(aData2()) To nrFields
            aDataAll(lRecs, lFields) = aData2(j)
        Next
    Next
End Sub

次の行でエラーが発生します: ReDim Preserve aDataAll(nrRecs, nrFields), これは、"nrFields" が負の値 (-1) に設定されているためです...

配列の上限が負の値である理由がわかりません...これに関するヘルプは大歓迎です。

4

3 に答える 3

1

私はそれを理解しました-ネストされたテーブルを抽出しようとしていました。すべてのサブテーブルを循環し、個別に抽出する必要がありました。^pまた、テーブル構造を保持するために、抽出する前に検索して削除する必要がありました。

それを理解した後、MS コード サンプルにエラーがあることに気付きました。aData2(j)実際にはaData2(lFields).

これが他の初心者に役立つことを願っています!

于 2013-06-12T14:11:53.713 に答える
1

UBound が -1 で LBound = 0 の場合、配列は空です。次のように空の配列を生成できます。

Dim EmptyArray() As String
Dim s As String
EmptyArray = Split("")
Debug.Print (UBound(EmptyArray)) ' displays -1
Debug.Print (LBound(EmptyArray)) ' displays 0

あなたの場合、配列が空の場合は処理をスキップする必要があると思われます:

aData1 = Split(...)
If (UBound(aData1) < LBound(aData1) Then
    ' UBound is -1 and LBound is 0, array is empty, nothing to do
Else
    ' Array is non-empty, do your stuff
End If
于 2013-06-12T14:47:53.507 に答える
0

非常に奇妙ですが、VARIANT SAFEARRAY では、任意の次元に対して負の下限値上限値を持つことができます。配列範囲は LBound(,dimension) から UBound(,dimension) です。

真でなければならないのは、UBound >= LBound です。

配列サイズを取得するには、UBound - LBound + 1 を使用します。

VBA コードの先頭にあるステートメントを使用して下限を設定するのが慣例Option Baseでしたが、もちろん、サード パーティのライブラリによって返される配列には影響しませんでした。ほとんどの人は、下限として 1 を使用していました。

于 2013-05-28T11:01:24.207 に答える