gpt4 book ai didi

vba - Next 没有 For/For 没有 Next 错误

转载 作者:行者123 更新时间:2023-12-03 08:14:28 24 4
gpt4 key购买 nike

我有以下代码......我之前在这里讨论过。虽然它一直在发展,所以我认为现在有点不同。

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 Next”错误。如果我在任何地方添加下一个,我会收到“下一个不为”错误。我想这与我的“如果……那么”陈述有关,但我并不完全确定。

有任何想法吗?

编辑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

最佳答案

  • 如上所述,下一步是缺失的。
  • Resolve_and_Open 这是本地命令吗?未声明为对象。
  • DeletetheDirectory 这是本地命令吗?或者这曾经被声明为一个函数?
  • 您需要适当的错误处理来收集代码中断的位置。
  • 您正在使用 (i) 的 Do While 循环下的嵌套 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

    关于vba - Next 没有 For/For 没有 Next 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22790366/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com