2014-03-19 24 views
0

我發現了一個想法,可能能夠解決我的問題,For ... Each ...循環。但是,這比典型的循環稍微複雜一些。如何使用複雜的For ...每個...循環(或更好的方法)?

我有三個模塊。

  1. 連接原始文件名,使用後綴i,從「-001」開始,每增加一個週期。

  2. 連接剛創建的新文件路徑,添加後綴i,從「-001」開始,每增加一個週期。

  3. 使用該程序在名爲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 

這仍然完全不是那麼回事。我收到了「預期的錯誤」,但它越來越接近!

Link to Forum

+0

原來這是我的屏幕對比度。我今天早上改了它。這裏是! :) – meer2kat

+0

我一直在試驗,所以它會顯得有點傻。但基本上,這用於顯示文件路徑的MsgBox,以便我可以確保它正確地連接了名稱。 – meer2kat

回答

1

看來簡單的答案將是聲明公共變量i。

Public i As Integer 

在你的模塊3,功能ReplaceComponent,然後你可以設置i=i+1在函數的末端,在兩個子OldNameiLoop和功能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 
+0

這解決了在多個模塊中使用我的問題,所以這太棒了!我仍然必須讓我的newnameiloop和oldnameiloop聽那個i + 1,但是......如何做到這一點的任何想法?我只用VBA工作了幾個星期。對新手問題抱歉! – meer2kat

+1

你不需要讓你的newnameiloop和oldnameiloop聽任何東西。在您的ReplaceComponent函數結束時,i = i + 1會增加您的公共變量1。那個公共變量然後用在你的newnameiloop和oldnameiloop中,因此已經使用增加的i(i + 1)。嘗試一下,讓我知道如果它仍然無法正常工作。 – Yoh

+0

我嘗試將它們全部組合到一個模塊中,因爲它不斷丟失「NewNamePath」。我上面寫了我的更改以及它造成的問題。 :/ – meer2kat