下一个没有For / For Without Next错误

时间:2014-04-01 15:31:34

标签: vba for-loop syntax-error

我有以下代码......我之前已在此讨论过。它虽然在不断发展,但我认为它现在有点不同了。

Option Explicit
Public i As Integer
Public oOccurrence As ComponentOccurrence

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

    Dim NameStr2 As String
    Dim OldNamePath As String

    NameStr = Renamer.New_Name.Text               'Concatenates the full new file path
    NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"

    NameStr2 = Renamer.Old_Name_Display.Text      'Concatenates the old file NAME
    OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt"


    'Creates a ton of errors that have been giving me a headache
    Dim oOcc As ComponentOccurrence

    For Each oOcc In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
        If oOcc.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then
            Set oOccurrence = oOcc
        End If
        Do While i < 99
            oOcc.Replace NewNamePath, True

            If i = 99 Then
                DeletetheDirectory
                                              'Will close the file
                Resolve_and_Open.Show vbModal                 'Reopens form 3 to select the next assembly
            Else:     
                For i = 1 To 99 Step 1
                Next

            End If
        Loop

End Sub

所以现在我得到一个&#34; For Without Next&#34; &#34; End Sub&#34;上的错误线。如果我在下一个地方添加任何地方,我会得到一个&#34; Next Without For&#34;错误。我想这与我的&#34;如果...然后&#34;有关。陈述,但我不完全确定。

有什么想法吗?


编辑1:

这是DeletetheDirectory模块。我没有遇到任何问题。我也在整个表格中使用它:

Option Explicit

' Delete this directory and all the files it contains.
  Sub DeletetheDirectory()

  Dim FSO
  Set FSO = CreateObject("Scripting.FileSystemObject")
  FSO.deletefolder "C:\\InventorTempFolder"

  On Error Resume Next

  End Sub

Resolve_and_Open是一个表单,我也没有遇到任何问题:

Private Sub Cancel_Click()

Unload Me                                                                        'Triggers cancel button
DeletetheDirectory

End Sub

Private Sub Open_Button_Click()

ThisApplication.SilentOperation = True                'Suppresses the resolve links dialog

Dim myPath As String
myPath = FileName.Text                                              'Gets the string, FileName, from module 1
Dim Shell As Object
Set Shell = CreateObject("Shell.Application")
Shell.Open (myPath)                                                      'Opens selected file

Resolve_and_Open.Hide                                               'Hides module
ReplaceComponent

'ReplaceComponent will go here once it works

End Sub

Private Sub OpenAssemblies_Click()

SelectFileOpenDialog                                                    'Calls to OpenFileDialog Module

End Sub

3 个答案:

答案 0 :(得分:2)

For Each oOcc ...

需要相应的

Next oOcc

答案 1 :(得分:2)

  1. 如上所述缺少下一个。
  2. Resolve_and_Open这是一个本地命令吗?未声明为对象。
  3. 删除目录这是一个本地命令吗?或者这被宣布为一种功能?
  4. 您需要正确的错误处理来收集代码中断的位置。
  5. 您正在使用(i)在Do While循环下的嵌套for循环中重新激活变量(i)。
  6. 试试此VBS并发布您的错误回复。

        Option Explicit
        Public i As Integer
        Public oOccurrence As Object
    
        Public Sub ReplaceComponent()
    
        On Error Resume Next
        Dim NameStr As String
        Dim NewNamePath As String
    
        Dim NameStr2 As String
        Dim OldNamePath As String
    
        NameStr = Renamer.New_Name.Text               'Concatenates the full new file path
        NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"
        if err.number<>0 then msgbox "Error Found After NewNamePath:" & err.description
        err.clear
    
        NameStr2 = Renamer.Old_Name_Display.Text      'Concatenates the old file NAME
        OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt"
        if err.number<>0 then msgbox "Error Found After OldNamePath:" & err.description
        err.clear
    
        'Creates a ton of errors that have been giving me a headache
        Dim oOcc As Object
        if err.number<>0 then msgbox "Error Found After oOcc:" & err.description
        err.clear
    
        Dim Occs As Object : Set Occs = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
        if isArray(Occs) then
        For k=0 to Ubound(Occs)
            msgbox "Activated object, verifying object properties: " & oOcc.Name
            if err.number<>0 then msgbox "Could not activate object."
            err.clear
            If oOcc.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then
                Set oOccurrence = oOcc
            End If
            if err.number<>0 then msgbox "Error Found After oOccurrence declaration:" & err.description
            err.clear 
    
            Do While i < 99
                oOcc.Replace NewNamePath, True
                if err.number<>0 then msgbox "Error Found After oOcc.Replace:" & err.description
                err.clear
    
                If i = 99 Then
                    DeletetheDirectory
                    if err.number<>0 then msgbox "Error Found After DeleteTheDirectory:" & err.description
                    err.clear
                                                  'Will close the file
                    Resolve_and_Open.Show vbModal                 'Reopens form 3 to select the next assembly
                    if err.number<>0 then msgbox "Error Found After Resolve_and_Open:" & err.description
                    err.clear
                Else:     
                    For j = 1 To 99 Step 1
                    Next
    
                End If
            Loop
         Next oOcc
         Else
                 Msgbox "Occurrences does not contain an Array" 
         End If
        End Sub
    

答案 2 :(得分:1)

使用

For Each oOcc ...
    ...
Next oOcc

这个循环的重点是什么:

Else:     
            For i = 1 To 99 Step 1
            Next