“运行时间错误92:对于不初始化的循环”第一次成功后的错误
我应该修改前同事的工具。这将基于Excel表(第1列=标题,第2列=文本)创建一个Word文档。从长远来看,Excel表应该可以扩展。可以通过复选框选择创建哪些章节。 但是,如果使用该工具不止一次,则运行时间错误92发生。由于我将蒙版与文档中也插入的数据一起使用,因此不必重新启动该工具。我是一个完整的初学者,对小费感到非常高兴。在循环之后,是一个格式化标题的块。也许这也是错误所在的地方。 非常感谢!
我在这里看到了类似的问题,不幸的是,这对我没有帮助。 无法解决运行时错误92:对于未初始化的循环。有什么想法吗?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在下一个X之后放置错误goto0。当前您的错误语句将在下一个X之后导致代码中的错误,以跳回错误标签。 (在for循环内)for循环完成后。
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.