For ... Each ...循环语法/运行时错误91 /无效的下一个控制变量参考(或“为什么这个程序不运行?”)

时间:2014-03-20 11:34:40

标签: vba foreach syntax-error

我正在回复"预期:"以下代码行出错。语法错了吗?

For Each oOcc As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences 

根据MSDN,它应该遵循:

For Each element [ As datatype ] In group
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]

感谢simoco提供错误帮助!

Sub ReplaceComponent()
   Dim NameStr As String
   Dim NewNamePath As String

   Dim NameStr2 As String
   Dim OldNamePath As String

  For i = 0 To 99 Step 1
  NameStr = Renamer.New_Name.Text
  NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"

  NameStr2 = Renamer.Old_Name_Display.Text
  OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt"

  Dim oOcc As ComponentOccurrence
  For Each oOcc In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
     If oOcc.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then
        Set oOccurrence = oOcc
        Exit For
     End If
  Next oOcc


  oOccurrence.Replace NewNamePath, True
   Next i
End Sub

现在它声称oOcc没有被定义,所以它不起作用,但我确定将来有一天,如果他们解决了这个问题,我希望能够看到这个问题。同一个项目。

嗯......所以我试过了。

Newest

现在它让我一路走到"接下来oOcc"在它生气之前告诉我"无效的下一个控制变量参考"。善良这件事变化无常。我不知道我是否正确定义了这些东西,但它至少停止给我一个错误。

  Dim oOccurrence As ComponentOccurrence
  Dim oOcc As Object
  For Each oOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
    If oOccurrence.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then
    Set oOccurrence = oOcc
    oOcc = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
        Exit For
     End If
  Next oOcc

   Next i
End Sub

0 个答案:

没有答案