2014-04-01 56 views
0

我有以下代碼...我已經在這裏討論過。它一直在演變,所以現在有點不同了,我想。下一個沒有/沒有下一個錯誤

Option Explicit 
Public i As Integer 
Public oOccurrence As ComponentOccurrence 

Public Sub ReplaceComponent() 
    Dim NameStr As String 
    Dim NewNamePath As String 

    Dim NameStr2 As String 
    Dim OldNamePath As String 

    NameStr = Renamer.New_Name.Text    'Concatenates the full new file path 
    NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt" 

    NameStr2 = Renamer.Old_Name_Display.Text  'Concatenates the old file NAME 
    OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt" 


    'Creates a ton of errors that have been giving me a headache 
    Dim oOcc As ComponentOccurrence 

    For Each oOcc In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences 
     If oOcc.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then 
      Set oOccurrence = oOcc 
     End If 
     Do While i < 99 
      oOcc.Replace NewNamePath, True 

      If i = 99 Then 
       DeletetheDirectory 
               'Will close the file 
       Resolve_and_Open.Show vbModal     'Reopens form 3 to select the next assembly 
      Else:  
       For i = 1 To 99 Step 1 
       Next 

      End If 
     Loop 

End Sub 

所以現在我在「End Sub」行上得到「For Without Without」錯誤。如果我在下一個地方添加,則會收到「Next Without For」錯誤。我想這與我的「如果...然後」的陳述有關,但我不完全確定。

任何想法?


編輯1:

這裏是DeletetheDirectory模塊。我沒有問題。我用它在我的形式,以及:

Option Explicit 

' Delete this directory and all the files it contains. 
    Sub DeletetheDirectory() 

    Dim FSO 
    Set FSO = CreateObject("Scripting.FileSystemObject") 
    FSO.deletefolder "C:\\InventorTempFolder" 

    On Error Resume Next 

    End Sub 

而且Resolve_and_Open是一個形式,我也沒有任何問題:

Private Sub Cancel_Click() 

Unload Me                  'Triggers cancel button 
DeletetheDirectory 

End Sub 

Private Sub Open_Button_Click() 

ThisApplication.SilentOperation = True    'Suppresses the resolve links dialog 

Dim myPath As String 
myPath = FileName.Text            'Gets the string, FileName, from module 1 
Dim Shell As Object 
Set Shell = CreateObject("Shell.Application") 
Shell.Open (myPath)              'Opens selected file 

Resolve_and_Open.Hide            'Hides module 
ReplaceComponent 

'ReplaceComponent will go here once it works 

End Sub 

Private Sub OpenAssemblies_Click() 

SelectFileOpenDialog             'Calls to OpenFileDialog Module 

End Sub 
+0

外部For沒有下一個,所以你應該只需要把它放在End Sub之前。 什麼是:在Else聲明之後?這只是一個錯字嗎? – DoctorMick

+0

它會自動將其更改爲實際值。不知道爲什麼。當我把它放在結束sub之前,我得到一個無效的下一個控制變量引用錯誤 – meer2kat

+0

你應該刪除結尾的冒號。 VBA會認爲這是一個標籤。 – DeanOC

回答

2
  1. 如上所述,缺少下一個。
  2. Resolve_and_Open是一個本地命令嗎?沒有被聲明爲一個對象。
  3. DeletetheDirectory是一個本地命令嗎?或者這是否曾被宣佈爲一種功能?
  4. 您需要正確的錯誤處理來收集代碼破壞的位置。
  5. 您正在使用(i)的Do While Loop下的嵌套for循環中重新激活變量(i)。

試試這個VBS併發布錯誤回覆。

Option Explicit 
    Public i As Integer 
    Public oOccurrence As Object 

    Public Sub ReplaceComponent() 

    On Error Resume Next 
    Dim NameStr As String 
    Dim NewNamePath As String 

    Dim NameStr2 As String 
    Dim OldNamePath As String 

    NameStr = Renamer.New_Name.Text    'Concatenates the full new file path 
    NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt" 
    if err.number<>0 then msgbox "Error Found After NewNamePath:" & err.description 
    err.clear 

    NameStr2 = Renamer.Old_Name_Display.Text  'Concatenates the old file NAME 
    OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt" 
    if err.number<>0 then msgbox "Error Found After OldNamePath:" & err.description 
    err.clear 

    'Creates a ton of errors that have been giving me a headache 
    Dim oOcc As Object 
    if err.number<>0 then msgbox "Error Found After oOcc:" & err.description 
    err.clear 

    Dim Occs As Object : Set Occs = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences 
    if isArray(Occs) then 
    For k=0 to Ubound(Occs) 
     msgbox "Activated object, verifying object properties: " & oOcc.Name 
     if err.number<>0 then msgbox "Could not activate object." 
     err.clear 
     If oOcc.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then 
      Set oOccurrence = oOcc 
     End If 
     if err.number<>0 then msgbox "Error Found After oOccurrence declaration:" & err.description 
     err.clear 

     Do While i < 99 
      oOcc.Replace NewNamePath, True 
      if err.number<>0 then msgbox "Error Found After oOcc.Replace:" & err.description 
      err.clear 

      If i = 99 Then 
       DeletetheDirectory 
       if err.number<>0 then msgbox "Error Found After DeleteTheDirectory:" & err.description 
       err.clear 
               'Will close the file 
       Resolve_and_Open.Show vbModal     'Reopens form 3 to select the next assembly 
       if err.number<>0 then msgbox "Error Found After Resolve_and_Open:" & err.description 
       err.clear 
      Else:  
       For j = 1 To 99 Step 1 
       Next 

      End If 
     Loop 
    Next oOcc 
    Else 
      Msgbox "Occurrences does not contain an Array" 
    End If 
    End Sub 
+0

失去了第3步,失去了第4步,無法激活對象,錯誤oOccurrence聲明後發現:對象變量或未設置塊變量 – meer2kat

+0

根本找不到應用程序或者找不到組件? – meer2kat

+1

您的應用程序找不到「ThisApplication.ActiveDocument.ComponentDefinition'對象,這就是爲什麼它不能遍歷事件 – Rich

2
For Each oOcc ... 

需要一個相應的

Next oOcc 
+0

這讓我一個運行時錯誤91,對象變量沒有設置:(我也想到了這一點 – meer2kat

1

使用

For Each oOcc ... 
    ... 
Next oOcc 

也這是什麼循環的點:

Else:  
      For i = 1 To 99 Step 1 
      Next 

+0

我想到了。其他人也是如此。這是我在下面的迴應:「這讓我一個運行時錯誤91,對象變量沒有設置:(我也想到了這一點: – meer2kat