3 Replies Latest reply on Jun 27, 2018 3:23 PM by Keaton Warn

    Can I save PDFs of Subassemby/Component Drawings With Only the Main Assembly Drawing Open?

    Keaton Warn

      Is it possible to open a main assembly drawing and then run a macro that saves it as a PDF, but also finds all of the sub-assembly and component drawings and saves those as PDFs as well? I've been looking up posts, googling, and trying out code all afternoon but have had no luck.


      The closest I got was last week when I started trying to write this macro. I made a dynamic array of the file names contained in the assembly and a loop that went through the array and saved a PDF under each name, but the differently-named PDF files themselves were all of the main assembly since that was the open document. I've tried finding a way to access these sub-assembly/parts to save as PDFs but have had no luck. Optimally they wouldn't be opened in a window in SolidWorks (just in memory) so as to improve performance and not clutter up the user's SolidWorks window every time the macro is run.


      Thank you to all of the guys who have helped me with macros so far - your help has been appreciated!

        • Re: Can I save PDFs of Subassemby/Component Drawings With Only the Main Assembly Drawing Open?
          Fifi Riri

          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


          Sub main()

              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)

              TraverseComponent swRootComp

              PrintDrawing swAssy.GetPathName

          End Sub


          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

                  TraverseComponent swChildComp

                  PrintDrawing FilePath


          End Sub


          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

                  MsgBox "Nothing"

                  Exit Sub

              End If


              swExportPDFData.ViewPdfAfterSaving = False

              boolstatus = swDraw.Extension.SaveAs(FileName & ".PDF", 0, 0, swExportPDFData, longstatus, longwarnings)

              swApp.CloseDoc swDraw.GetTitle

          End Sub

          • Re: Can I save PDFs of Subassemby/Component Drawings With Only the Main Assembly Drawing Open?
            Keaton Warn

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

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

                End If

            Next i

            Debug.Print "    Loop Successful."




            Here are the Immediate Window results: