【发布时间】:2014-04-25 23:23:48
【问题描述】:
我找到了一个可以解决我的问题的想法,即 For...Each... 循环。但是,它比典型的循环要复杂一些。
我有三个模块。
使用后缀 i 连接原始文件名,从“-001”开始,每个循环加一。
连接刚刚创建的新文件路径,添加后缀 i,从“-001”开始,每个循环加一。
使用该程序将旧文件替换为名为 Autodesk Inventor 的程序中的新文件。
问题是我需要第三个模块来替换组件,然后告诉模块一和模块二移动到下一个 i。我认为 for....each... 循环可能能够做到这一点,但我不确定如何使其工作,因为它将 i 从其他两个模块而不是它自己的模块中引用。有人有什么想法吗?
我可以尝试从我的三个模块中发布我的代码,但由于某种原因,格式现在不听我的。
无论如何,有人要求我发布它。希望它会重新格式化自己。
模块 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
这仍然无法正常工作。我收到“预期:在”错误,但它越来越近了!
【问题讨论】:
-
原来是我的屏幕对比度。今天早上我换了。这里是! :)
-
我一直在试验,所以看起来有点傻。但基本上,这用于显示文件路径的 MsgBox,因此我可以确保它正确连接名称。