首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用复杂的For...Each.循环(或者更好的方法)?

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

Stack Overflow用户
提问于 2014-03-19 15:25:23
回答 1查看 130关注 0票数 0

我找到了一个可以解决我的问题的想法,For...Each.循环播放。然而,它比一个典型的循环要复杂一些。

我有三个模块。

  1. 连接原始文件名,使用后缀i,从"-001“开始,每个周期增加一个。
  2. Concatenates刚刚创建的新文件路径,添加一个后缀i,从"-001“开始,每个周期增加一个。
  3. 使用该程序将旧文件替换为名为Autodesk Inventor的程序中的新文件。

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

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

有人要求我把它寄出去。希望它能重新格式化自己。

单元1:

代码语言:javascript
运行
复制
    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:

代码语言:javascript
运行
复制
    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:

代码语言:javascript
运行
复制
    Option Explicit

    Public Function ReplaceComponent()

    Dim oOccurrence As ComponentOccurrence
    Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath
    oOccurrence.Replace NewNamePath, True

    End Function

这里有更多信息:发明家论坛

我把它们结合在一起:

代码语言:javascript
运行
复制
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。是我做错了你的改变,还是这是一个全新的问题?这是错误行..。

代码语言:javascript
运行
复制
Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath

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

代码语言:javascript
运行
复制
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

这个还是不太好用。我得到了一个“期望:在”错误,但它越来越近了!

链接到论坛

EN

回答 1

Stack Overflow用户

发布于 2014-03-19 16:44:20

似乎简单的答案是声明一个公共变量i.

代码语言:javascript
运行
复制
Public i As Integer

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

确保从Sub OldNameiLoop和NewNameiLoop函数中删除NewNameiLoop。

不需要用一个for-每个循环我相信。

编辑:更详细的,我建议这样做,以改进您的最新版本:

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22510553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档