Reputation: 133
I have a working script that saves multiple Solidworks parts through a loop using vba. The amount and name of parts is dependent on the user that selects parts to be saved in a userform.
If files are already saved in the folder with the same name, a messagebox pops up with a Do you want to overwrite this file
questions with either yes
(overwrites the file) and no
(cancels and goes back to subform). The yes
options works fine. However, when I select no
, the entire statement cancels. Therefore it is never an option to NOT overwrite part1 and to DO overwrite part2, because the messagebox for part2 never occurs. How can I make this happen? Hopefully this image clears out my problem. You can also find my accessory code below.
If you have any further questions, or I need to clarify something more, please let me know.
'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
Upvotes: 1
Views: 70
Reputation: 54807
Yes
in the message box.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
Upvotes: 2