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

VBA循环保存SolidWorks零件:取消覆盖后需继续处理后续零件

解决SolidWorks VBA批量保存时跳过已存在文件的问题

问题核心是原代码在用户选择“不覆盖”时用Exit Sub直接终止了整个过程,导致后续零件无法处理。只需将终止整个过程的逻辑改为跳过当前循环迭代即可实现需求。

修改后的代码片段

'Save the file if it does not exist yet
Dim FileNameOverwrite
If Not Dir(finalName, vbDirectory) = vbNullString Then
    FileNameOverwrite = MsgBox("Filename " & finalNameCut & " already exists. Do you want to overwrite?", vbQuestion + vbYesNo, "File overwrite")
    UserParam.Hide
    If FileNameOverwrite = vbNo Then
        UserParam.Show
        GoTo SkipCurrentSave ' 跳过当前零件的保存,直接进入下一次循环
    End If
End If
 
swModelToExport.Extension.SaveAs3 finalName, 0, 1, Nothing, Nothing, nErrors, nWarnings
 
'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

SkipCurrentSave: ' 循环跳转标签,用于跳过当前零件的保存流程
Next

End Sub

关键改动说明

  1. 将Exit Sub替换为GoTo SkipCurrentSave,这样不会终止整个宏,只是跳过当前零件的保存步骤,直接进入下一次循环迭代。
  2. 在Next语句前添加SkipCurrentSave:标签,作为跳转目标,确保跳过当前零件后能正确进入下一个循环。

这样修改后,当用户选择“no”时,程序会跳过当前零件的覆盖保存操作,继续处理后续的零件,而不会终止整个批量处理流程。

内容的提问来源于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 04:04:52