vbaloopssaveoverwritesolidworks

Overwrite files msgbox within a loop using Solidworks vba


I have a script that saves x amount parts of a SW assembly into step/xt, based on the input of a userform. This works really well. However, I want to add a messagebox that asks "Do you want to overwrite this file" as soon as the user saves a file that already exists in the folder. This must be part of a loop, so that they can determine it for every single part.

In my script below, I tried something, but if I "un-comment" it, my script does not save anything at all. Can anyone give me advice on how to improve it so that it will work?

ArrayList contains the parts the user wants to save, determined by checkboxes, and is different everytime.

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
        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: The shown code works now. ADDITIONALLY, I have another follow-up question.

If I save multiple files that already exist, I want the user to determine if they want to overwrite it for each file. With the current code, this is happening: enter image description here

Intentionally I saved part 1 and part 4, but the names already exists. Therefore, the Do you want to overwrite messagebox pops up. If I say I want to overwrite, it automatically goes on to the other double files. If I cancle however, it goes back to the userform. In this case, it is never an option to choose to overwrite "Part 4" after I selected "no" for the first part.

How can I adapt my code, so that it also loops through all parts when I cancel? Thank you in advance.


Solution

  • Question: but if I "un-comment" it, my script does not save anything at all

    Pls try.

    'Save the file if it does not exist yet
        Dim FileNameOverwrite
        If Not Dir(finalName, vbDirectory) = vbNullString Then
            FileNameOverwrite = MsgBox("This filename already exists. Do you want to overwrite?", vbQuestion + vbYesNo, "File overwrite")
            If FileNameOverwrite = vbNo Then
                MsgBox "Dit doet nog niks"
                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