13 Replies Latest reply on Oct 18, 2013 7:04 AM by Michael De Jager

    API to save as PDF to network folder

    Tony Hayes

      Hello Everyone,


      I am wondering if it is possible to have a macro that will save drawings as a PDF to a network file folder. I need this to work when the user saves a drawing it will automatically save the pdf also. I do not want to use task scheduler because I would need to run that every night. I need this so that the latest drawing is on our network folder. We do not use pdmwork group. I also do not want it to be a second operation where the user runs a macro, this will lead to someone forgetting to run it and then we would not have the latest drawing on the network. The people that need these pdf's will be getting quotes for these parts and they do not have Autocad or Solidworks on their systems.

        • Re: API to save as PDF to network folder
          Paul Marsman

          Yes, it is possible.

          • Re: API to save as PDF to network folder
            Paul Marsman

            look at these save events for drawings:



              • Re: API to save as PDF to network folder
                Tony Hayes

                Would anyone have a sample of something like this?

                  • Re: API to save as PDF to network folder
                    Paul Marsman

                    here is my macro for saving a drawing to pdf in the same folder as the pdf.  The filename works for us since we don't have "." in our filenames, if that is not the case for you then some modification will be required.  Also, if you are always saving to the same folder you can hardcode that pretty easy.  The only thing is this doesn't have the save event in it... I've never done an event for SolidWorks but it isn't hard to do in Excel and such.  If you need help with that let me know.


                    ' ******************************************************************************
                    ' This Macro will save the active drawing sheet as a PDF file in the same directory
                    ' ******************************************************************************

                    Dim swApp As SldWorks.SldWorks
                    Dim swDrawing As DrawingDoc
                    Dim swModExtension As ModelDocExtension
                    Dim swExportData As ExportPdfData
                    Dim sheetsFileNames As Variant
                    Dim sheetsFileName As Variant
                    Dim lWarnings As Long
                    Dim lErrors As Long
                    Dim sFileName As String
                    Dim strings As Variant

                    Sub SavePDF()

                    Set swApp = Application.SldWorks
                    Set swDrawing = swApp.ActiveDoc
                    Set swModExtension = swDrawing.Extension

                    If swDrawing Is Nothing Then
                        MsgBox ("Drawing must be open")
                        Exit Sub
                    End If

                    sFileName = swDrawing.GetPathName() 'Gets file sFileName with path
                    If sFileName = "" Then
                        MsgBox ("Please save the drawing before PDF export")
                        Exit Sub
                        strings = Split(sFileName, ".")
                        sFileName = strings(0) & ".PDF"
                    End If
                    'MsgBox (sFileName)

                    Set swExportData = swApp.GetExportFileData(swExportDataFileType_e.swExportPdfData)

                    boolstatus = swExportData.SetSheets(swExportDataSheetsToExport_e.swExportData_ExportCurrentSheet, Nothing)
                    'boolstatus = swExportData.SetSheets(swExportDataSheetsToExport_e.swExportData_ExportAllSheets, 1)

                    'save the file
                    boolstatus = swModExtension.SaveAs(sFileName, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, 0, 0)

                    If boolstatus Then
                        MsgBox "PDF Saved"
                        MsgBox "Save as PDF failed, Error code:" & lErrors
                    End If
                    Set swApp = Nothing
                    Set swmodel = Nothing
                    Set swModelDocExt = Nothing
                    Set swExportData = Nothing
                    End Sub