MACRO LIBRARY

Pack and go to folder

Free public access. No account or subscription required.

Files

Pack and go to folder

Code

Module: mMain

'Demonstrates how to pack and go a model to a folder.
    
'Preconditions: Model is open and output folder is specified.
'Results: Model is exported via pack and go to specified folder.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Const OUTPUT_FOLDER As String = "C:\Temp\test\"
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    Dim strOutputFolder As String
    strOutputFolder = OUTPUT_FOLDER
    
    If SaveAsPackAndGo(swApp, swModel, strOutputFolder) = False Then Exit Sub
End Sub
    
Function SaveAsPackAndGo(swApp As SldWorks.SldWorks, _
    swModel As SldWorks.ModelDoc2, strOutputFolder As String) As Boolean
    
    On Error GoTo ErrHandler
    
    Dim swPackAndGo As SldWorks.PackAndGo
    Dim intDocCount As Integer
    Dim vPathNames As Variant
    Dim strSetPathNames() As String
    Dim i As Integer
    Dim vStatus As Variant
    Dim strRetVal As String
    
    Set swPackAndGo = swModel.Extension.GetPackAndGo
    swPackAndGo.SetSaveToName True, strOutputFolder
    vStatus = swModel.Extension.SavePackAndGo(swPackAndGo)
    
    'Determine whether pack and go was successful
    For i = 0 To UBound(vStatus)
        Select Case vStatus(i)
            Case 0
                strRetVal = "Pack and go succeeded"
            Case 1
                strRetVal = "User input not correct"
            Case 2
                strRetVal = "File already exists"
            Case 3
                strRetVal = "File to save is empty"
            Case 4
                strRetVal = "Save error"
        End Select
        
        If vStatus(i) <> 0 Then
            swApp.SendMsgToUser "Pack and go fail reason: " & vbCrLf & strRetVal
            SaveAsPackAndGo = False
            Exit For
        Else
            SaveAsPackAndGo = True
        End If
        
    Next i
    
    Exit Function
    
ErrHandler:
    Debug.Print "ERROR: " & Err.Number & " " & Err.Description
    swApp.SendMsgToUser "Unhandled error occurred while saving pack and go."
    SaveAsPackAndGo = False
End Function