0

I want to save multiple files at once. Based on checkboxes in a userform, an array called ArrayParts is created. Each checkbox contains the name of the file that the user wants to save.

My code gets the pathname of the openend SW assembly and cuts off everything after the last slash (1), opens a userform (2) that collects user information that will later on be used to name the to be saved parts (3). The parts.SLDPRTS the user wants to save are individually openend and each should get their own name. Subsequently, the script contains the function NumPart that copies a specific code in my initial pathname and uses that code in the new partname. The parts can be saved either as .step or .x_t file. This is the code I explained.

'Declare variables
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Dim Step As Long
Dim PathInit, PathCut  As String
Dim instance As ISldWorks

Sub SaveFiles()

    'Use opened assembly as active document
    Set swApp = Application.SldWorks
    Set Part = swApp.ActiveDoc
    
    'Prepare path
    PathInit = Part.GetPathName 'Determine file location of the assembly
    PathCut = Left(PathInit, InStrRev(PathInit, "\")) 'Remove text "Assembly.SLDASS" after the last slash
    
    'Open user form
    UserParam.Show
    
End Sub

Public Sub UserInput(InputMldPartNrFS, InputMldPartNrMS, InputREVCodeNr, InputCheckBxFS, InputCheckBxMS, OptionExtension, InputArrayParts As String)
    Dim MldPartNrFS, MldPartNrMS, REVCodeNR, CheckBxFS, CheckBxMS As String
    Dim ArrayList As Variant
    Dim ExtInit, ExtNew, PartNameFS, ProjectNr, XTFolder, REV  As String
    Dim PartNr, initName As String
    Dim SavePart As Variant
    Dim i As Integer
    
    '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)
        initName = PathCut + ArrayList(i) + ExtInit

        ' FIXED SIDE
        Set Part = swApp.OpenDoc6(initName, 1, 0, "", longstatus, longwarnings)
        SavePart = Part.SaveAs3(PathCut & "XT\" & NumPart(initName) & "_" & MldPartNrFS & " " & ArrayList(i) & " " & REVCodeNR & ExtNew, 0, 2)
        
        'Reopen assembly
        swApp.ActivateDoc2 "Mld_Assembly.SLDASM", False, longstatus
        Set Part = swApp.ActiveDoc
    Next
        
End Sub

Function NumPart(initName As String) As String
  Dim FileName As String, FoldPath As String, arr, arrEl, El
  Dim newFileName As String, fileNoExtention As String
  
  arr = Split(initName, "\")
  For Each El In arr
    arrEl = Split(El, "_")
    If UBound(arrEl) = 2 Then
        If IsNumeric(arrEl(1)) Then
            NumPart = arrEl(1): Exit For
        End If
    End If
  Next El
  
End Function

The userform looks like this: enter image description here

The problem with my code lies within my loop. What does work is selecting a checkbox and saving that specific name, however I cannot save multiple files at once. When selecting multible boxes, I get a Runtime error "91": Object variable or With block variable not set. It debugs towards the following line:

SavePart = Part.SaveAs3(PathCut & "XT\" & PartNr & "_" & MldPartNrFS & " " & ArrayList(i) & " " & REVCodeNR & ExtNew, 0, 2)

Does anyone see my fault and is willing to help me? Thank you in advance.

5
  • With your edit of the question, you really have 2 questions here. For the first question, instead of using PartNr in the SaveAs3 method try using changeFileName(initName, ExtInit). For the second question, it sounds like arr doesn't contain any elements. What are you passing in for initName? Set a breakpoint on the line in question and inspect the values for initName and arr. Commented Aug 13, 2024 at 12:26
  • @Brian M Stafford Thank you lots. This already improved parts of my code and makes it clearer. I used changeFileName in the SaveAs method and that works quite well. I renamed some of the terms and also rewrote my question. Hopefully it gets clearer everytime..! If I select for example "Name1, Name2 and Name7" in the checkboxes, and print InputArrayParts then it prints Name1, Name2, Name7. Still, I don't manage to save multiple parts with this code. Commented Aug 15, 2024 at 14:40
  • Nothing obvious sticks out to me. The only object I see is Part... I would step through the code and make sure Part isn't Nothing the second time through the loop. Commented Aug 15, 2024 at 16:40
  • @BrianMStafford If the part is nothing the second time through the loop, then what could I do to solve it? Or does it have to do with my initName that could be corrupt? Commented Aug 19, 2024 at 6:42
  • Whenever you are using objects don't assume that they are valid. You should always check them. So in this case do If Not Part Is Nothing Then SavePart = ... Commented Aug 19, 2024 at 11:13

1 Answer 1

1

In your main sub you can do something like this:

Dim i As Integer
Dim HERE As Variant
   
HERE = Split(InputArrayParts, ", ")
   
For i = LBound(HERE) To UBound(HERE)
   Set Part = swApp.OpenDoc6(initName & HERE(i) & ".SLDPRT", 1, 0, "", longstatus, longwarnings)
   SavePart = Part.SaveAs3(PathCut & XTFolder & PartNr & "_" & MPartNrFS & " " & HERE(i) & " " & CodeNR & ".STEP", 0, 2)
Next

First, convert your input string into an array using Split. Then loop through the array and build up the needed parms for the open and save lines.

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

1 Comment

thank you for your comment. This is somewhat what I am looking for. However, I'm not sure how to implement this through my function. My loop should go back and forth in my function. I adapted my question., hopefully you can understand my quetsion. Can you help me once again?

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.