如何使用复杂的For ... Each ...循环(或更好的方法)?

时间:2014-03-19 15:25:24

标签: vba loops file-io foreach

我找到了一个可以解决我的问题的想法,For ... Each ...循环。但是,它比典型的循环稍微复杂一些。

我有三个模块。

  1. 使用后缀i连接原始文件名,从" -001&#34开始;每个周期增加一个。

  2. 连接刚刚创建的新文件路径,添加后缀i,从" -001&#34开始;每个周期增加一个。

  3. 使用程序将旧文件替换为名为Autodesk Inventor的程序中的新文件。

  4. 问题是我需要第三个模块来替换组件,然后告诉模块一和模块二移动到下一个模块。我认为....每个...循环可能能够做到这一点,但我不知道如何使这项工作,因为它将从其他两个模块而不是自己的模块引用我。有人有什么想法吗?

    我可以尝试从我的三个模块发布我的代码,但由于某种原因,格式化现在没有听我说。

    有人要求我发布它。希望它会重新格式化。

    第1单元:

        Option Explicit
    
        Public Sub OldNameiLoop()
    
        Dim i As Double
        Dim NameStr2 As String
        Dim OldNamePath As String
    
        NameStr2 = Renamer.Old_Name_Display.Text
        OldNamePath = NameStr & "-" & Right("00" & i, 3) & ".ipt"
    
        Do While i < 99
        i = i + 1
        If 'Something Happens Here' Then
        '3-character string created by using the Right() function
        Next i
        Else: Exit Sub
        End If
    
        Loop
        End Sub
    



    第2单元:

        Option Explicit
    
        Public Function NewNameiLoop()
    
        Dim i As Double
        Dim NameStr As String
        Dim NewNamePath As String
    
    
        NameStr = Renamer.New_Name.Text
        NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"
    
        Do While i < 99                               'Counts with the file name up to -099
        i = i + 1
            If 'Something happens here' Then
        Loop
    
            Else: Exit Function
    
            End If
    
        End Function
    



    第3单元:

        Option Explicit
    
        Public Function ReplaceComponent()
    
        Dim oOccurrence As ComponentOccurrence
        Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath
        oOccurrence.Replace NewNamePath, True
    
        End Function
    

    此处有更多信息: Inventor Forum




    我把它们全部合并到了这里:

    Option Explicit
    Public i As Integer
    
    
    Public Function ReplaceComponent()
    
    
    Dim NameStr As String
    Dim NewNamePath As String
    
    Dim NameStr2 As String
    Dim OldNamePath As String
    
    
    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 oOccurrence As ComponentOccurrence
    Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath
    oOccurrence.Replace NewNamePath, True
    
    Do While i < 99
    i = i + 1
    
    Loop
    
    End Function
    

    但它现在遇到了错误91.我是否错误地进行了更改或这是一个全新的问题?这是错误行..

    Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath
    

    编辑2(来自Inventor自定义论坛):

    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 oOccurrence As ComponentOccurrence 
      For Each oOcc As ComponentOccurrence in ThisApplication.ActiveDocument.ComponentDefinition.Occurrences 
         If oOcc.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then 
            Set oOccurrence = oOcc 
            Exit For
         End If 
      Next oOcc
    
      'Then you can replace
      oOccurrence.Replace NewNamePath, True
       Next i
    End Sub
    

    这仍然无法奏效。我得到了一个&#34;预期:&#34;错误,但它越来越近了!

    Link to Forum

1 个答案:

答案 0 :(得分:1)

似乎简单的答案是声明公共变量i。

Public i As Integer

在模块3,函数ReplaceComponent中,您可以在函数末尾设置i=i+1,并在Sub OldNameiLoop和Function NewNameiLoop中进一步使用此变量。

确保从Sub OldNameiLoop和Function NewNameiLoop中删除Dim i As Double

我认为不需要使用for-each循环。


编辑:更详细我会建议这样的东西来改进你的最新版本:

Option Explicit



Public Function ReplaceComponent()

Dim i as integer 'no need to declare public if you put everything in one function
Dim NameStr As String
Dim NewNamePath As String

Dim NameStr2 As String
Dim OldNamePath As String


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" 'not sure if correct, I think you need to add Renamer.Path_Text.text here just like for your NewNamePath above.



Dim oOccurrence As ComponentOccurrence
Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath 
'Not sure why you get the error, but maybe because of what you I commented above for   
'OldNamePath. Otherwise post the error here as well, including the contents of OldNamePath 
'at the moment of the error.
oOccurrence.Replace NewNamePath, True

Do While i < 99 'This entire do while loop does nothing in your function except for adding
i = i + 1       'up i untill it is 99. Then it just exits your function. If you want to
                'repeat the entire process 99 times, you want to put this first line right below
Loop            'the last Dim-line

End Function