0

I have written the following VBA code to save my Solidworks files. It also checks if files already exists in the folder and if so - asks through a messagebox if these files need to be overwritten. This is the code:

Main sub()
PathInit = Part.GetPathName                         'Determine file location of the assembly
PathCut = Left(PathInit, InStrRev(PathInit, "\"))   'Remove text "Assembly.SLDASS" after the last slash
initName = PathCut + ArrayList(i) + ExtInit                                                                                                                                     'Name to open the original file
finalName = PathCut & FolderName & "\" & NumPart(initName) & "_" & mldpartcode & " " & ArrayListAdapted & " " & "[REV" & UserParam.getREV(UserParam, CStr(ArrayListNr(i))) & "]" & ExtNew  'New filename
finalNameCut = NumPart(initName) & "_" & mldpartcode & " " & ArrayListAdapted & " " & "[REV" & UserParam.getREV(UserParam, CStr(ArrayListNr(i))) & "]" & ExtNew

 For i = LBound(ArrayList) To UBound(ArrayList) 'Run loop x times depending on the amount of selected checkboxes in the userform
    '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 + vbYesNoCancel, "File overwrite")
        UserParam.Hide
        If FileNameOverwrite = vbNo Then
            UserParam.Show
            IsToBeSaved = False
            'Exit Sub ' Stop the code execution, no more looping
        End If
        If FileNameOverwrite = vbCancel Then
            UserParam.Show
            Exit Sub
        End If
    End If
    
    If IsToBeSaved Then
        swModelToExport.Extension.SaveAs3 finalName, 0, 1, Nothing, Nothing, nErrors, nWarnings
    End If
    
    'Close all the files
    swApp.CloseDoc ArrayList(i) & ".SLDPRT"
     
    '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

This works pretty good, except for overwriting of multiple files. Lets say I already saved two files called "Saved1" and "Saved2". Now, I save the two exact same files again and want the code to ask me if I want to overwrite each file or not with the following pop-up:

enter image description here

If I choose the option "Yes", than the code works fine and a similar messagebox pops up, but for Saved2 this time. However, if I select the option 'No' for the first file ("Saved1"), then it goes just back to my userform, instead of asking if I want to overwrite Saved2, even if I did not want to overwrite Saved1.

Does anyone know how to adapt the code, so that is asks if I want to overwrite the second file, EVEN THOUGH I do not want to overwrite the first file?

Thank you in advance

9
  • Is this sub called several times? Or how are Saved1 and Saved2 defined/allocated/looped? Commented Oct 9, 2024 at 15:34
  • If you add a break point and step through the code with the SW Macro Editor by pressing F8 (F8 makes steps to next line when pressed), then you can walk through how your program is acting vs how you think it should be acting Commented Oct 9, 2024 at 16:41
  • Quite a few items in your posted code seem to be coming from somewhere else (e.g. ArrayList, i, finalName, finalNameCut), so it's not very clear exactly how the code is called and in what context. Commented Oct 9, 2024 at 21:40
  • @TimWilliams I understand the confusion. ArrayList(i) are partnames that the user checked through checkboxes that must be saved. So i is depending on checkboxes (for now 1-8 options). finalName and finalNameCut are the names of the files (including directory), and are generated based on the ArrayList. So if the user clicked 7 parts, then the loop will run 7 times and save 7 files with all different names. Commented Oct 10, 2024 at 6:25
  • 1
    Can you try removing the line UserParam.Show in your loop before you set IsToBeSaved = False? You're saying that you don't want to go back to the userform when you click no but then you tell your loop to show the form when you click no Commented Oct 10, 2024 at 13:54

1 Answer 1

1

Seems like this (untested) might be closer to what you want:

Sub Main()
    
    Dim FileNameOverwrite
    
    PathInit = Part.GetPathName                         'Determine file location of the assembly
    PathCut = Left(PathInit, InStrRev(PathInit, "\"))   'Remove text "Assembly.SLDASS" after the last slash

    UserParam.Hide
    
    For i = LBound(ArrayList) To UBound(ArrayList)
 
        initName = PathCut + ArrayList(i) + ExtInit                                                                                                                                     'Name to open the original file
        
        'filename
        finalNameCut = NumPart(initName) & "_" & mldpartcode & " " & _
                    ArrayListAdapted & " " & _
                    "[REV" & UserParam.getREV(UserParam, CStr(ArrayListNr(i))) & "]" & ExtNew
        
        finalName = PathCut & FolderName & "\" & finalNameCut  'full path
        
        'Save the file if it does not exist yet
        If Not Dir(finalName, vbNormal) = vbNullString Then
            
            FileNameOverwrite = MsgBox("Filename " & finalNameCut & _
                          " already exists. Do you want to overwrite?", _
                          vbQuestion + vbYesNoCancel, "File overwrite")
            
            Select Case FileNameOverwrite
                Case vbYes
                    swModelToExport.Extension.SaveAs3 finalName, 0, 1, _
                                  Nothing, Nothing, nErrors, nWarnings
                Case vbNo         'do nothing - just process next file
                Case vbCancel     'user cancelled any other saves
                    UserParam.Show
                    Exit Sub
            End Select
        End If
        swApp.CloseDoc ArrayList(i) & ".SLDPRT" 'Close this file
     Next i
     
    '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

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much. It looks much more understandable in your code. However, in this case, it does not save any files at all. Perhaps I am missing something. I will have another look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.