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

Time:05-19

I am supposed to revise a tool of a former colleague. This creates a Word document based on an Excel table (column 1 = heading, column 2 = text). The Excel table should be expandable in the long run. Which chapters are created can be selected by check boxes. However, if the tool is used more than once, the run time error 92 occurs. Since I use a mask with data that is also inserted in the document, the tool should not have to be restarted. I am a complete beginner and would be very happy about a tip. After the For-loop is a block that formats the headings. Maybe this is also where the error lies. Many thanks in advance!

I have seen a similar problem here, unfortunately it did not help me. Can't solve Run-time error 92: For loop not initialized. Any idea?

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

CodePudding user response:

Try putting an On Error Goto 0 after the Next X. Currently your on error statement will cause an error in the code after the Next x to jump back to the error label. (inside the for loop) after the for loop has completed.

  • Related