Add and Remove Files from Pack and Go (VBA)
This example shows how to add SolidWorks, render reference, and
non-SolidWorks files to Pack and Go. This example also shows how to remove a
non-SolidWorks file from Pack and Go.
'-------------------------------------------
' Preconditions:
' 1. Specified assembly exists.
' 2. The folder, c:\temp, exists.
' 3. Open the Immediate window.
'
' Postconditions:
' 1. Names of SolidWorks, render reference, and
' non-SolidWorks files for Pack and Go are
' printed to the Immediate window.
' 2. Name of non-SolidWorks file to remove is
' printed to Immediate window.
' 3. SolidWorks, render reference, and
' non-SolidWorks files are packed up and
' copied to c:\temp.
' 4. Examine c:\temp to verify.
'-------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swPackAndGo As SldWorks.PackAndGo
Dim openFile As String
Dim errors As Long
Dim warnings As Long
Dim status As Boolean
Dim pgFileNames As Variant
Dim i As Long
Dim renderReferences As Variant
Dim otherFiles(1) As String
Dim delOtherFiles(0) As String
Dim myPath As String
Dim statuses As Variant
Sub main()
Set swApp = Application.SldWorks
' Open assembly document
openFile = "C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\EDraw\claw\claw-mechanism.sldasm"
Set swModel = swApp.OpenDoc6(openFile, swDocASSEMBLY, swOpenDocOptions_Silent, "", errors, warnings)
Set swModelDocExt = swModel.Extension
' Get Pack and Go object
Debug.Print "Pack and Go"
Set swPackAndGo = swModelDocExt.GetPackAndGo
' Get current paths and filenames of the assembly's documents
status = swPackAndGo.GetDocumentNames(pgFileNames)
Debug.Print ""
Debug.Print " Add SolidWorks files' paths and filenames: "
If (Not (IsEmpty(pgFileNames))) Then
For i = 0 To UBound(pgFileNames)
Debug.Print " The path and filename is: " & pgFileNames(i)
Next i
End If
' Set document paths and names for Pack and Go
status = swPackAndGo.SetDocumentSaveToNames(pgFileNames)
' Get the render stock references in this assembly
' and print them to the Immediate window
Debug.Print " "
renderReferences = swModelDocExt.GetRenderStockReferences
Debug.Print " Add render references:"
For i = 0 To UBound(renderReferences)
Debug.Print " The path and filename is: " & renderReferences(i)
Next i
' Add render stock files to Pack and Go
status = swPackAndGo.AddExternalDocuments(renderReferences)
' Add other non-SolidWorks files to Pack and Go
otherFiles(0) = "c:\program files\solidworks corp\solidworks\samples\tutorial\edraw\claw\claw-mechanism.easm"
otherFiles(1) = "c:\program files\solidworks corp\solidworks\samples\tutorial\edraw\claw\claw-mechanism.emodel_debugonly.xml"
Debug.Print " "
Debug.Print " Add non-SolidWorks files:"
For i = 0 To UBound(otherFiles)
Debug.Print " The path and filename is: " & otherFiles(i)
Next i
status = swPackAndGo.AddExternalDocuments(otherFiles)
' Remove one of the non-SolidWorks files from Pack and Go
Debug.Print " "
Debug.Print " Remove non-SolidWorks file:"
delOtherFiles(0) = otherFiles(0)
Debug.Print " The path and filename is: " & delOtherFiles(0)
status = swPackAndGo.RemoveExternalDocuments(delOtherFiles)
' Override path where to save documents
myPath = "c:\temp\"
status = swPackAndGo.SetSaveToName(True, myPath)
' Pack and Go both SolidWorks and non-SolidWorks files
statuses = swModelDocExt.SavePackAndGo(swPackAndGo)
End Sub