3 Replies Latest reply on Apr 1, 2016 11:19 AM by Yong Ning

    Print PDF WITH Api

    Yong Ning

      Hope use print PDF file  with API method → create PDF file.

       

      PDF.jpg

       

      Save or Print to PDF, how's it working for you? | SOLIDWORKS Forums

      https://forum.solidworks.com/message/438349#438349

        • Re: Print PDF WITH Api
          Yong Ning

          Now use SaveAs4 → the code is :

           

           

          ''

          Function ModelToPDF(SwModel As ModelDoc2)

            Dim AcrApp As Acrobat.AcroApp, AvDoc As Acrobat.AcroAVDoc

              Set AcrApp = CreateObject("AcroExch.App")

              Set AvDoc = CreateObject("AcroExch.AvDoc")

            

            Dim E As Long, W As Long, sFileName

              sFileName = SwModel.GetPathName

              sFileName = Left(sFileName, Len(sFileName) - 6) & "Pdf"

              AvDoc.Open sFileName, ""

              AcrApp.Show

              AcrApp.CloseAllDocs  

              ''

              Debug.Print SwModel.GetTitle

            

              SwModel.SaveAs4 sFileName, 0, 0, E, W

              'Debug.Print

              AvDoc.Open sFileName, ""  

          End Function

          ********************************

          Help me,  Create PDF file of use other method.

          see first picture.

            • Re: Print PDF WITH Api
              Yong Ning

              Sub RngSavePDF()

                  Dim T: T = Timer

                  Dim Xls As Excel.Application, Rng As Range

                      Set Xls = GetObject(, "Excel.Application")

                      Set Rng = Xls.Selection

                  Dim Sht As Worksheet

                      Set Sht = Rng.Parent

                  Dim swApp As SldWorks.SldWorks, SwModel As ModelDoc2

                      Set swApp = Application.SldWorks

                  Dim SwDraw As DrawingDoc

                   

                  Dim E As Long, W As Long, Str

                  Dim PdfDwgPath, SldDrwPath, xlsPath, openSldDrw, saveSldDrw, SavePDF, SaveEdrw

                      xlsPath = Xls.ActiveWorkbook.Path '& "\"

                      Debug.Print Sht.Name

                      PdfDwgPath = xlsPath & Sht.Cells(3, 1)

                      SldDrwPath = xlsPath & Sht.Cells(3, 2)

                      openSldDrw = SldDrwPath & Sht.Cells(4, 2)

                      For ii = 1 To Rng.Rows.Count

                          Debug.Print "Start Open SldDrw",

                          ''

                          openSldDrw = PdfDwgPath & Rng(ii, 1) & ".SldDrw"

                          Debug.Print openSldDrw,

                          Set SwModel = SwOpenFile(swApp, openSldDrw)

                          ''

                          SavePDF = Replace(UCase(SwModel.GetPathName), ".SLDDRW", ".PDF")

                          SaveEdrw = Replace(UCase(SwModel.GetPathName), ".SLDDRW", ".eDrw")

               

               

                          If swApp.RevisionNumber <> "14.0.0" Then

                             SwModel.SaveAs SavePDF

                          End If

                          ''

                          If swApp.RevisionNumber = "14.0.0" Then

                             SwModel.SaveAs SaveEdrw

                          End If

                         

                          'Stop

                          swApp.CloseDoc SwModel.GetPathName

                          ''

                          Debug.Print SavePDF,

                          PrintTiming T

                      Next ii

                     

              End Sub

            • Re: Print PDF WITH Api
              Sunil Badkar

              There already API help available for this function.

              2013 SOLIDWORKS API Help - Save File as PDF Example (VBA)

               

              Option Explicit

                  Dim swApp               As SldWorks.SldWorks
                  Dim swModel             As SldWorks.ModelDoc2
                  Dim swModelDocExt       As SldWorks.ModelDocExtension
                  Dim swExportPDFData     As SldWorks.ExportPdfData
                  Dim boolstatus          As Boolean
                  Dim filename            As String
                  Dim lErrors             As Long
                  Dim lWarnings           As Long
                  Dim strSheetName(4)     As String
                  Dim varSheetName        As Variant

              Sub main()

                  ' Path to which to save PDF file of drawing
                  filename = "C:\foodprocessor.PDF"

                  Set swApp = Application.SldWorks
                  swApp.Visible = True

                  ' Open specified drawing
                  Set swModel = swApp.OpenDoc6("c:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\advdrawings\foodprocessor.slddrw", swDocDRAWING, swOpenDocOptions_Silent, "", lErrors, lWarnings)
                  Set swModelDocExt = swModel.Extension
                  Set swExportPDFData = swApp.GetExportFileData(1)

                  ' Names of the sheets
                  strSheetName(0) = "Sheet1"
                  ' strSheetName(1) = "Sheet2"
                  ' strSheetName(2) = "Sheet3"
                  ' strSheetName(3) = "Sheet4"

                  varSheetName = strSheetName

                  If swExportPDFData Is Nothing Then MsgBox "Nothing"
                  boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varSheetName)
                  swExportPDFData.ViewPdfAfterSaving = True

                  boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportPDFData, lErrors, lWarnings)

              End Sub