JetskiS
JetskiS

Reputation: 133

Files overwriting messagebox in a loop using VBA

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. enter image description here

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

EDIT enter image description here

Upvotes: 1

Views: 70

Answers (1)

VBasic2008
VBasic2008

Reputation: 54807

Implement a Boolean to Decide Whether to Save a File

  • It is assumed that you want to save the file when it doesn't exist, and if it exists, when the user selects Yes in the message box.
  • Sorry, I don't know SolidWorks so I used only the lines I'm familiar with.
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

Related Questions