运行时错误91初始化数组

时间:2014-02-28 20:03:31

标签: vba word-vba

在模块中:

Option Explicit

Sub sortLoggingEntrys()

    Dim entrys As entrys
    entrys.initialize (ActiveDocument.Tables.Count)
    Dim tableIndex As Integer
    Dim i As Integer
    Dim j As Integer

    For i = 1 To ActiveDocument.Paragraphs.Count
      If InStr(ActiveDocument.Paragraphs(i), "text over table") Then
        'Check if table is in next 3 paragraphs
        For j = i To (i + 3)
            If ActiveDocument.Paragraphs(j).Range.Information(wdWithInTable) = True Then
                tableIndex = ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Tables.Count
                entrys.add DateValue(Left(ActiveDocument.Paragraphs(i), 10)), ActiveDocument.Tables(tableIndex + 1)
                Exit For
            End If
        Next
     End If
    Next
End Sub

在课堂模块中:

Option Explicit

Dim mdate() As Date
Dim mtable() As Table
Dim index As Integer

Public Sub initialize(arraySize As Integer)
    ReDim mdate(arraySize)
    ReDim mtable(arraySize)
    index = 1
End Sub

Public Function getDate(ByVal ix As Integer) As Date
    Set getDate = mdate(ix)
End Function

Public Function getTable(ByVal ix As Integer) As Table
    Set getTable = mtable(ix)
End Function

Sub add(ByVal dat As Date, ByVal tabl As Table)
    mdate(index) = dat
    mtable(index) = tabl
    index = index + 1
End Sub

这是我用Word制作的第一个VBA脚本。我得到运行时错误91.在调试模式下,我发现初始化过程中发生了错误。甚至可以在调试模式下跳转到类模块?我无法弄清楚如何跳进程序。此外,我不知道为什么会发生错误。谷歌无法帮助我,希望你能。

1 个答案:

答案 0 :(得分:1)

DIM类型为entrys的变量之后,您必须将其设置为新实例。

Dim entrys As entrys
Set entrys = New entrys