You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SolidWorks VBA循环中逐文件覆盖确认功能优化问询

解决SolidWorks VBA导出零件时“不覆盖”终止循环的问题

你的核心问题是在用户选择“不覆盖”时用了Exit Sub,直接终止了整个子过程,导致后续零件无法处理。只需要把退出逻辑改成跳过当前循环迭代,而不是退出整个程序即可。

修改后的完整代码

Public Sub UserInput(InputMldPartNrFS, InputMldPartNrMS, InputREVCodeNr, InputCheckBxFS, InputCheckBxMS, OptionExtension, InputArrayParts As String)
    
    'Create new pathname
    ExtInit = ".SLDPRT"                         'Old extension
    ExtNew = OptionExtension                    'Input from userform: either X_T or STEP
    MldPartNrFS = InputMldPartNrFS              'Input from userform
    MldPartNrMS = InputMldPartNrMS              'Input from userform
    REVCodeNR = "[REV" + InputREVCodeNr + "]"   'Input from userform
    ArrayList = Split(InputArrayParts, ",")
        
    For i = LBound(ArrayList) To UBound(ArrayList) 'Run loop x times depending on the amount of selected checkboxes in the userform
        
        'Assign moldcode number on either FS or MS parts
        Select Case ArrayList(i)
          Case "part1", "part2", "part3", "part4"        'If part contains these names
              mldpartcode = MldPartNrFS                                                                 'Then assign the code for FS
        Case Else                                                                                   'If the names are not as written above
              mldpartcode = MldPartNrMS                                                                 'Then assign the code for MS
        End Select
        
        initName = PathCut + ArrayList(i) + ExtInit
        finalName = PathCut & "XT\\" & NumPart(initName) & "_" & mldpartcode & " " & ArrayList(i) & " " & REVCodeNR & ExtNew
        Debug.Print "finalName = ", finalName
        'Open and activate the correct model
        Set swModel = swApp.OpenDoc6(initName, 1, 0, "", nStatus, nWarnings)                                                'Open the model
        Set swModelActivated = swApp.ActivateDoc3(initName, False, swRebuildOnActivation_e.swUserDecision, nErrors)         'Activate the model
        Set swModelToExport = swApp.ActiveDoc                                                                               'Get the activated model
        Debug.Print "strModelName = ", strModelName
        
        'Save the file if it does not exist yet
        Dim FileNameOverwrite
        ' 修正Dir函数参数:用vbNormal判断文件存在,避免误判目录
        If Not Dir(finalName, vbNormal) = vbNullString Then
            FileNameOverwrite = MsgBox("Filename " & finalName & " already exists. Do you want to overwrite?", vbQuestion + vbYesNo, "File overwrite")
            UserParam.Hide
            If FileNameOverwrite = vbNo Then
                UserParam.Show
                ' 先关闭当前打开的零件文档,避免占用资源
                swApp.CloseDoc initName
                ' 跳转到循环末尾,继续处理下一个零件
                GoTo NextIteration
            End If
        End If
         
        swModelToExport.Extension.SaveAs3 finalName, 0, 1, Nothing, Nothing, nErrors, nWarnings
        ' 关闭导出后的零件文档
        swApp.CloseDoc initName
         
        'Reopen assembly
        Set swModel = swApp.OpenDoc6(PathInit, 1, 0, "", nStatus, nWarnings)                                                'Open the model
        Set swModelActivated = swApp.ActivateDoc3(PathInit, False, swRebuildOnActivation_e.swUserDecision, nErrors)         'Activate the model
        Set swModelToExport = swApp.ActiveDoc                                                                                 'Get the activated model
        
        ' 循环迭代标签:用于跳过当前零件后继续下一个
NextIteration:
    Next
        
End Sub

关键修改点说明

  • 替换退出逻辑:把Exit Sub改成GoTo NextIteration,跳转到循环末尾的标签,这样只会跳过当前零件,循环会继续处理下一个。
  • 修正文件存在判断:原代码用vbDirectory作为Dir函数的参数,会匹配同名目录,改成vbNormal专门判断文件是否存在,更准确。
  • 优化资源占用:在跳过或导出完成后,调用swApp.CloseDoc initName关闭当前零件文档,避免SolidWorks打开过多文档导致性能问题。
  • 修正提示文本:原代码中finalNameCut未定义,直接用finalName显示完整路径,让用户明确知道哪个文件存在。

内容的提问来源于stack exchange,提问作者JetskiS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 01:57:02