我有一个工作脚本,可以使用 vba 通过循环保存多个 Solidworks 零件。部件的数量和名称取决于选择要保存在用户表单中的部件的用户。
如果文件已保存在具有相同名称的文件夹中,则会弹出一个消息框,其中包含
Do you want to overwrite this file
问题,其中包含 yes
(覆盖文件)和 no
(取消并返回子表单)。 yes
选项效果很好。但是,当我选择 no
时,整个语句都会取消。因此,不覆盖第 1 部分并覆盖第 2 部分永远不是一个选项,因为第 2 部分的消息框永远不会出现。我怎样才能做到这一点?希望这张图片能解决我的问题。您还可以在下面找到我的配件代码。
如果您还有任何其他问题,或者我需要澄清更多内容,请告诉我。
'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
Exit Sub ' Stop the code execution, no more looping
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
Next
End Sub
Yes
时保存。For...
'Save the file if it does not exist yet
Dim FileNameOverwrite
Dim IsToBeSaved
IsToBeSaved = True
If Not Dir(finalName, vbDirectory) = vbNullString Then
FileNameOverwrite = MsgBox("Filename " & finalNameCut _
& " already exists. Do you want to overwrite?", _
vbQuestion + vbYesNo + vbDefaultButton2, "File overwrite")
UserParam.Hide
If FileNameOverwrite = vbNo Then
UserParam.Show
IsToBeSaved = False
End If
End If
If IsToBeSaved Then
' your 'SaveAs' line
End If
'Reopen assembly
' the last 3 lines
Next