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 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.

If Not Part Is Nothing Then SavePart = ...