我發現了一個想法,可能能夠解決我的問題,For ... Each ...循環。但是,這比典型的循環稍微複雜一些。如何使用複雜的For ...每個...循環(或更好的方法)?
我有三個模塊。
連接原始文件名,使用後綴i,從「-001」開始,每增加一個週期。
連接剛創建的新文件路徑,添加後綴i,從「-001」開始,每增加一個週期。
使用該程序在名爲Autodesk Inventor的程序中用新文件替換舊文件。
問題是,我需要第三個模塊來替換組件,然後告訴模塊一和模塊二移動到下一個我。我認爲一個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
這裏有更多的信息: 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
這仍然完全不是那麼回事。我收到了「預期的錯誤」,但它越來越接近!
原來這是我的屏幕對比度。我今天早上改了它。這裏是! :) – meer2kat
我一直在試驗,所以它會顯得有點傻。但基本上,這用於顯示文件路徑的MsgBox,以便我可以確保它正確地連接了名稱。 – meer2kat