Hello, Try this with an opened assembly file.
Dim swApp As SldWorks.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Dim longstatus As Long, longwarnings As Long
Set swApp = Application.SldWorks
Dim swConf As SldWorks.Configuration
Dim swRootComp As SldWorks.Component2
Set swAssy = swApp.ActiveDoc
Set swConf = swAssy.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponent3(True)
Sub TraverseComponent(swComp As SldWorks.Component2)
Dim vChilds As Variant, vChild As Variant
Dim swChildComp As SldWorks.Component2
Dim MyString As String
vChilds = swComp.GetChildren
For Each vChild In vChilds
Set swChildComp = vChild
Dim FilePath As String
FilePath = swChildComp.GetPathName
Sub PrintDrawing(FileName As String)
FileName = Left(FileName, InStr(FileName, ".") - 1)
If Dir(FileName & ".SLDDRW") = "" Then Exit Sub
Dim swDraw As SldWorks.ModelDoc2
Set swDraw = swApp.OpenDoc6(FileName & ".SLDDRW", 3, 0, "", longstatus, longwarnings)
Dim swExportPDFData As SldWorks.ExportPdfData
Set swExportPDFData = swApp.GetExportFileData(1)
If swExportPDFData Is Nothing Then
swExportPDFData.ViewPdfAfterSaving = False
boolstatus = swDraw.Extension.SaveAs(FileName & ".PDF", 0, 0, swExportPDFData, longstatus, longwarnings)
I think I found another viable way to achieve what I am looking to do, but for some reason this makes a lot more sense than the "traverse" method code I was looking at yesterday. I'll keep this on file since it seems to do what I want, just in a very different way. Thanks!
If I can get my method to work, I will post it in this thread.
Here is an excerpt of the code I made. It opens the subassembly/subpart drawing files, but closes them after the PDF is saved. I will try to find a way to avoid that later on if it becomes an issue. I am using some static file locations here that I will be changing to variables and/or other static locations.
'Note: pgFileNames is a dynamic array of strings that has had the file names of the assmebly entered into it.
Dim strFileType As String
Dim strFileName As String
Dim LErrors As Long
Dim LWarnings As Long
Dim strNameTempt As String
Dim swDraw As SldWorks.DrawingDoc
Set swDraw = swModelDoc
For i = 0 To UBound(pgFileNames)
Debug.Print "The path and filename is: " & pgFileNames(i)
strFileName = pgFileNames(i)
strFileType = Right(strFileName, 6)
Debug.Print "strFileType is: " & strFileType
If strFileType = "SLDDRW" Then
Debug.Print "strFileType If_Statement Started."
Set swModelDoc = swApp.OpenDoc6("U:\Engineering Automation\Crate-B05.SLDDRW", swDocDRAWING, swOpenDocOptions_Silent, "", LErrors, LWarnings)
strNameTempt = CStr(i)
swDraw.SaveAs ("C:\temp\Crate-B05" + strNameTempt + ".PDF")
swApp.CloseDoc ("U:\Engineering Automation\Crate-B05.SLDDRW")
Debug.Print "PDF Saved. strFileType If Statement Ended."
Debug.Print "No PDF Made."
Debug.Print " Loop Successful."
Here are the Immediate Window results: