The one below works for assemblies (SLDASM, SLDPRT), but for some reason it doesn't work for drawings (SLDDWG) files. What needs to be changed for it to work? I'm attaching the SW macro as well as there is some userform data in the macro itself, and it is more legiable that the copy and paste below.
The purpose of this macro is to be able to pack and go one or more assemblies in one operation. I need a simlair version for drawings.
'------------------------------------------
'
' Preconditions:
' 1. Specified assembly exists. Does not work on parts or drawings.
' 2. The folder listed in FolderFrom exist.
'
' Postconditions:
' 1. Examine FolderTo to verify.
' 2. All letters are switched to lowercase
'
'-------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swPackAndGo As SldWorks.PackAndGo
Dim openFile As String
Dim myFileName As String
Dim pgFileNames As Variant
Dim pgFileStatus As Variant
Dim pgSetFileNames() As String
Dim pgGetFileNames As Variant
Dim pgDocumentStatus As Variant
Dim status As Boolean
Dim warnings As Long
Dim errors As Long
Dim i As Long
Dim j As Long
Dim k As Long
Dim namesCount As Long
Dim myPath As String
Dim statuses As Variant
Public FolderFrom As String
Public AssemNames As String
Public FolderTo As String
Sub main()
Dim NameAry() As String
Dim count1 As Long
Dim splitstring1() As String
UserForm1.Show
NameAry = Split(AssemNames, ",")
count1 = UBound(NameAry)
Set swApp = Application.SldWorks
For k = 0 To count1
' Open assembly
openFile = FolderFrom & "\\" & NameAry(k)
Set swModelDoc = swApp.OpenDoc6(openFile, swDocASSEMBLY, swOpenDocOptions_Silent, "", errors, warnings)
Set swModelDocExt = swModelDoc.Extension
' Get Pack and Go object
Debug.Print "Pack and Go"
Set swPackAndGo = swModelDocExt.GetPackAndGo
' Get number of documents in assembly
namesCount = swPackAndGo.GetDocumentNamesCount
Debug.Print " Number of model documents: " & namesCount
status = swPackAndGo.GetDocumentNames(pgFileNames)
Debug.Print ""
Debug.Print " Current path 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
' Get current save-to paths and filenames of the assembly's documents
status = swPackAndGo.GetDocumentSaveToNames(pgFileNames, pgFileStatus)
Debug.Print ""
Debug.Print " Current default save-to 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
' Folder where to save the files
myPath = FolderTo & "\\"
' setting filenames for the pack and go documents
ReDim pgSetFileNames(namesCount - 1)
Debug.Print ""
Debug.Print " My Pack and Go path and filenames before adding prefix and suffix: "
j = 0
For i = 0 To (namesCount - 1)
splitstring1 = Split(pgFileNames(i), "\\")
myFileName = splitstring1(UBound(splitstring1))
pgSetFileNames(i) = myPath & myFileName
Debug.Print " My path and filename is: " & pgSetFileNames(i)
Next i
' If a drawing document existed for the assembly or part document
' used in this example, then you have to ensure that the
' drawing document copied by Pack and Go references the assembly
' or part document copied by Pack and Go and not the original
' assembly or part document
' Calling IPackAndGo::SetSaveToName sets the target for drawings
' included in Pack and Go and overrides a call to
' IPackAndGo::SetDocumentSaveToNames
'status = swPackAndGo.SetSaveToName(True, myPath)
' Set document paths and names for Pack and Go
status = swPackAndGo.SetDocumentSaveToNames(pgSetFileNames)
' Add a prefix and suffix to the new Pack and Go filenames
'swPackAndGo.AddPrefix = "SW"
'swPackAndGo.AddSuffix = "PackAndGo"
' Verify document paths and filenames after adding prefix and suffix
ReDim pgGetFileNames(namesCount - 1)
ReDim pgDocumentStatus(namesCount - 1)
status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)
Debug.Print ""
Debug.Print " My Pack and Go path and filenames after adding prefix and suffix: "
For i = 0 To (namesCount - 1)
Debug.Print " My path and filename is: " & pgGetFileNames(i)
Next i
' Pack and Go
statuses = swModelDocExt.SavePackAndGo(swPackAndGo)
' Close last file
swApp.CloseDoc NameAry(k)
Next k
End Sub
SolidworksApi macros