"Run-time error 92: For loop not initialized"第一次成功后出错运行

"Run-time error 92: For loop not initialized" Error after the first successful run

我要修改一个前同事的工具。这将创建一个基于 Excel table 的 Word 文档(第 1 列 = 标题,第 2 列 = 文本)。 Excel table 应该可以在长 运行 中展开。可以通过复选框选择创建哪些章节。 但是,如果多次使用该工具,则会出现 运行 时间错误 92。由于我使用的掩码也插入了文档中的数据,因此不必重新启动该工具。我是一个完全的初学者,很乐意给小费。在 For-loop 之后是一个格式化标题的块。也许这也是错误所在。 非常感谢!

我在这里看到了类似的问题,不幸的是它没有帮助我。

With wdApp.Selection
'Text body is created from Excel
Dim oChild As Node
 For Each X In tv_Texte.Nodes
    If X.Checked And X.Children Then
            .InsertBreak 7
            .Style = wddoc.Styles("Überschrift 1")
            .TypeText Text:=X.Text
            .TypeParagraph
            Set oChild = X.Child
            Do
               On Error GoTo Error:
                If oChild.Checked Then
                'Search entry in table
                i = 1
                Do Until IsEmpty(Workbooks(1).Sheets(X.Text).Range("A" & i + 3)) Or (Workbooks(1).Sheets(X.Text).Range("A" & i + 3) = oChild.Text)
                    i = i + 1
                    Loop
                .Style = wddoc.Styles("Überschrift 2")
                .TypeText Text:=oChild.Text
                .TypeParagraph
                .Style = wddoc.Styles("Standard")
                If IsEmpty(Workbooks(1).Sheets(X.Text).Range("B" & i + 3).Value) Then
                       MsgBox "Fehler beim Importieren der Überschrift: " & vbCrLf & X.Text & " -> " & oChild.Text & vbCrLf & "Kein Text hinterlegt!"
                Else
                'Insert chapter text
                wdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
                .TypeText Text:=Replace(Workbooks(1).Sheets(X.Text).Range("B" & i + 3).Value, "vbTab", vbTab)
                .TypeParagraph
                End If
                End If
                Set oChild = oChild.Next
Error:
            Loop Until oChild Is Nothing
   
    End If
Next X
'This block creates the headings:
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(1)
        .NumberFormat = "%1"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(0.76)
        .TabPosition = wdUndefined
        .ResetOnHigher = 0
        .StartAt = 1
        .LinkedStyle = "Überschrift 1"
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(2)
        .NumberFormat = "%1.%2"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(1.02)
        .TabPosition = wdUndefined
        .ResetOnHigher = 1
        .StartAt = 1
        .LinkedStyle = "Überschrift 2"
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(3)
        .NumberFormat = "%1.%2.%3"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(1.27)
        .TabPosition = wdUndefined
        .ResetOnHigher = 2
        .StartAt = 1
        .LinkedStyle = "Überschrift 3"
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(4)
        .NumberFormat = "%1.%2.%3.%4"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(1.52)
        .TabPosition = wdUndefined
        .ResetOnHigher = 3
        .StartAt = 1
        .LinkedStyle = "Überschrift 4"
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(5)
        .NumberFormat = "%1.%2.%3.%4.%5"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(1.78)
        .TabPosition = wdUndefined
        .ResetOnHigher = 4
        .StartAt = 1
        .LinkedStyle = "Überschrift 5"
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(6)
        .NumberFormat = "%1.%2.%3.%4.%5.%6"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(2.03)
        .TabPosition = wdUndefined
        .ResetOnHigher = 5
        .StartAt = 1
        .LinkedStyle = "Überschrift 6"
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(7)
        .NumberFormat = "%1.%2.%3.%4.%5.%6.%7"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(2.29)
        .TabPosition = wdUndefined
        .ResetOnHigher = 6
        .StartAt = 1
        .LinkedStyle = "Überschrift 7"
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(8)
        .NumberFormat = "%1.%2.%3.%4.%5.%6.%7.%8"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(2.54)
        .TabPosition = wdUndefined
        .ResetOnHigher = 7
        .StartAt = 1
        .LinkedStyle = "Überschrift 8"
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(9)
        .NumberFormat = "%1.%2.%3.%4.%5.%6.%7.%8.%9"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(2.79)
        .TabPosition = wdUndefined
        .ResetOnHigher = 8
        .StartAt = 1
        .LinkedStyle = "Überschrift 9"
End With
        ListGalleries(wdOutlineNumberGallery).ListTemplates(1).Name = ""
        .Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
        ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
        DefaultListBehavior:=wdWord10ListBehavior
        .Delete Unit:=wdCharacter, Count:=1         'Empty chapter is created and deleted
End With

尝试在 Next X 之后放置一个 On Error Goto 0。目前,您的 on error 语句将导致 Next x 之后的代码出现错误以跳回错误标签。 (在 for 循环内)for 循环完成后。