我找到了一个可以解决我的问题的想法,For...Each.循环播放。然而,它比一个典型的循环要复杂一些。
我有三个模块。
问题是,我需要第三个模块来替换组件,然后告诉模块一和模块二移到下一个模块。循环可能能够做到这一点,但我不知道如何使它工作,因为它将引用我来自其他两个模块,而不是它自己的。有人有什么想法吗?
我可以尝试把我的代码从我的三个模块,但出于某种原因,格式没有听我现在。
有人要求我把它寄出去。希望它能重新格式化自己。
单元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这里有更多信息:发明家论坛
我把它们结合在一起:
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这个还是不太好用。我得到了一个“期望:在”错误,但它越来越近了!
链接到论坛
发布于 2014-03-19 16:44:20
似乎简单的答案是声明一个公共变量i.。
Public i As Integer在模块3 (函数ReplaceComponent )中,您可以在函数的末尾设置i=i+1,并在Sub OldNameiLoop和NewNameiLoop函数中进一步使用该变量。
确保从Sub OldNameiLoop和NewNameiLoop函数中删除NewNameiLoop。
不需要用一个for-每个循环我相信。
编辑:更详细的,我建议这样做,以改进您的最新版本:
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 Functionhttps://stackoverflow.com/questions/22510553
复制相似问题