2014-04-01 35 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