下一个没有?

时间:2014-03-21 13:50:44

标签: vba compiler-errors syntax-error

我确定这已被问了一百次,而且我已经将它与其他类似产品进行了比较,但我的代码中没有看到问题。现在它已经一路下滑到" Next oOccurrence"然后我得到一个" Next Without For"错误。我不明白......你呢?

Option Explicit
Public i As Integer

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               '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"


Dim oOccurrence As ComponentOccurrence               'Creates a ton of errors that have been giving me a headache
For Each oOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
    If oOccurrence.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then
    oOccurrence.Replace NewNamePath, True
Exit For
    End If

    If i = 99 Then
DeletetheDirectory                            'When i = 99, deletes the temporary directory
                                              'Will save the file
                                              'Will close the file
Resolve_and_Open.Show vbModal                 'Reopens form 3 to select the next assembly
    Else: Next oOccurrence
    Next i
    End If

End Sub

2 个答案:

答案 0 :(得分:1)

我不确定你要做什么但是下面的重新格式化可能会有所帮助。

我无法运行你的代码,因为我不知道Renamer是什么。你能解释一下这两个循环以及你希望它们结束时的内容吗?

Option Explicit
Public i As Integer

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

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

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

        'Creates a ton of errors that have been giving me a headache
        Dim invAD
        Set invAD = ThisApplication.ActiveDocument
        Dim oOccurrence As ComponentOccurrence
        Set oOccurrence = invAD.ComponentDefinition.ActiveOccurrence

        For Each oOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
            If oOccurrence.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then
                oOccurrence.Replace NewNamePath, True
                Exit For
            End If

            If i = 99 Then
                'When i = 99, deletes the temporary directory
                DeletetheDirectory

                'Reopens form 3 to select the next assembly
                Resolve_and_Open.Show vbModal
                Exit For
            End If
        Next oOccurrence
    Next i

End Sub

答案 1 :(得分:1)

我认为你的问题很简单,就是你在下一个之后我会结束。您应该首先关闭如果i = 99,并且根本不需要Else,因为无论i = 99是否都应该循环。

    If i = 99 Then
        DeletetheDirectory                                          
        Resolve_and_Open.Show vbModal               
    End if
    Next oOccurrence
Next i