10 Replies Latest reply on Dec 27, 2015 6:54 AM by Deepak Gupta

    pdf macro

    Angelo Flamingo

      Hello,

       

      I have created a macro to save a drawing as a pdf.  I have it saving in the current directory that the drawing is located as well.  The macro is functioning fine.

       

      What I am trying to do now to it is filtering the sheet titles so it only saves certain sheets as the pdf.  Our normal process is leaving the sheet titles as Sheet 1 and so on.  But we also rename only 1 of the sheets as DXF at the end of the sheets.  The DXF sheet is the only one that I want to exclude from the macro.

       

      Is this something that is possible?  If so does anyone have a code to perform this part of the macro?  Thank you in advance.  I have listed the macro that I have below.

       

      Public Enum swSaveAsOptions_e
          swSaveAsOptions_Silent = &H1    '  Save document silently or not
          swSaveAsOptions_Copy = &H2  '  Save document as a copy or not
          swSaveAsOptions_SaveReferenced = &H4    '  Save referenced documents or not (drawings and parts only)
      End Enum
         
          ' Define variable used to hold the SldWorks object
          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          Dim swDraw As SldWorks.DrawingDoc
          Dim Part As Object
          Dim sFileName As String
          Dim pltName As String
          Dim FNConfigName As Variant 'Name of the configuration (File Name)
          Dim FNCustInfoName As Variant 'Name of field (File Name)
          Dim RevNum As String 'Revision Number of file
          Dim RevConfigName As Variant 'Name of the configuration (Revision)
          Dim RevCustInfoName As Variant 'Name of field (Revision)

      ' Check the documentation in Visual Basic or Visual C++ for printer definitions
          Const vbPRORPortrait = 1
          Const vbPRORLandscape = 2
          ' Defined in swconst.bas
          Const swPrintPaperSize = 0
          Const swPrintOrientation = 1

      Option Explicit

      Sub CreatePDF()

          Set swApp = Application.SldWorks
          Set Part = swApp.ActiveDoc
          Set swModel = swApp.ActiveDoc
          ' These definitions are consistent with type names defined in swconst.bas
          Const swDocPART = 1
          Const swDocASSEMBLY = 2
          Const swDocDRAWING = 3
      ''' BEGIN CREATE PDF '''
      '' Define FILE PATH ''
          Dim FPath As String
          'FPath = "Current_dir"
          FPath = (CurDir + "\")
          Dim nErrors As Long
          Dim nWarnings As Long
          Dim sStartFileName As String 'Starting file name
             
      '************************************************************************
          ' Get File name of open drawing
          ' strip off SolidWorks file extension (slddrw)
          ' and add DXF extension (dxf)
          'sFileName = swModel.GetTitle 'Not Using this feature
          ' Now use this... Get File name from File>Properties Custom tab, PartNo field.
          FNConfigName = ""
          FNCustInfoName = "PartNo"
          sFileName = swModel.CustomInfo2(FNConfigName, FNCustInfoName)
      '************************************************************************
      'Capture path name for file
          sStartFileName = swModel.GetPathName()

      ' Get Revision Number
          RevConfigName = ""
          RevCustInfoName = "Revision"
          RevNum = swModel.CustomInfo2(RevConfigName, RevCustInfoName)
          'Left(string, length)
          RevNum = Left(RevNum, 2) 'Only get the first 2 characters in this field
      If swModel.GetType = swDocDRAWING Then
         If MsgBox("Are you sure you would like to create " + _
          vbCrLf + FPath + sFileName + "-" + RevNum & ".PDF ? ", vbYesNo + vbQuestion, "Create PDF?") = vbYes Then sFileName = sFileName + "-" + RevNum
             
              'Plot to FILE name
              pltName = FPath + sFileName + ".pdf"
              ' Save to PDF
              Part.SaveAs4 pltName, 0, swSaveAsOptions_Silent, nErrors, nWarnings

              Else
              Exit Sub

      End If


      End Sub

          • Re: pdf macro
            John Stoltzfus

            I have a question on the link that you shared.....

             

            What do I need to remove and add so the pdf get's saved in the same folder as the drawing, right now the code has a path that needs to be modified if I want it to go in the same folder as the drawing/assembly and parts...

             

             

              • Re: pdf macro
                Deepak Gupta

                Remove following line

                filename = "C:\xxxxx.PDF"

                 

                And following line above boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportPDFData, lErrors, lWarnings)

                 

                filename = Left(swModel.GetPathName, Len(swModel.GetPathName) - 6) & "PDF"

                  • Re: pdf macro
                    John Stoltzfus

                    "Thanks a lot"

                    • Re: pdf macro
                      Angelo Flamingo

                      Thank you for your help!

                       

                      I got the pdf to save without the DXF sheet by utilizing your link. 

                       

                      My question now is how do I get the revision to be inserted at the end of the filename.  I tried to put the code in that I have but it does not seem to like it.  Thanks!

                        • Re: pdf macro
                          Deepak Gupta

                          From where you want to use the REV value?

                            • Re: pdf macro
                              Angelo Flamingo

                              I was pulling the rev value from the custom properties.  I was using the code below to get the value.  Then I was stringing the filename, - and revnum to get the final file name to save as. 

                               

                                  Dim RevNum As String 'Revision Number of file

                                  Dim RevConfigName As Variant 'Name of the configuration (Revision)

                                  Dim RevCustInfoName As Variant 'Name of field (Revision)

                               

                              ' Get Revision Number

                                  RevConfigName = ""

                                  RevCustInfoName = "Revision"

                                  RevNum = swModel.CustomInfo2(RevConfigName, RevCustInfoName)

                                  'Left(string, length)

                                  RevNum = Left(RevNum, 2) 'Only get the first 2 characters in this field

                               

                              sFileName = sFileName + "-" + RevNum

                                • Re: pdf macro
                                  Deepak Gupta

                                  Please share the full codes (better attach the macro here) to see where it is failing.

                                    • Re: pdf macro
                                      Angelo Flamingo

                                      I was using the code that you had provided and was looking to put in the get revision code.

                                       

                                      Sub main()

                                       

                                       

                                          Dim swApp               As SldWorks.SldWorks

                                          Dim swModel             As SldWorks.ModelDoc2

                                          Dim swDrawDoc           As SldWorks.DrawingDoc

                                          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()      As String

                                          Dim varSheetName        As Variant
                                         
                                          ' Path to which to save PDF file of drawing

                                          filename = (CurDir + "\")

                                         

                                          Set swApp = Application.SldWorks

                                          swApp.Visible = True

                                       

                                          Set swModel = swApp.ActiveDoc

                                          Set swDrawDoc = swModel

                                          Set swModelDocExt = swModel.Extension

                                       

                                          Set swExportPDFData = swApp.GetExportFileData(swExportDataFileType_e.swExportPDFData)

                                       

                                          ReDim strSheetName(0)

                                          Dim s As Variant

                                          For Each s In swDrawDoc.GetSheetNames

                                              If Not UCase(s) Like "*DXF*" Then

                                                  strSheetName(UBound(strSheetName)) = s

                                                  ReDim Preserve strSheetName(UBound(strSheetName) + 1)

                                              End If

                                          Next s

                                         

                                          varSheetName = strSheetName
                                         


                                          If swExportPDFData Is Nothing Then MsgBox "Nothing"

                                          boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varSheetName)

                                      filename = Left(swModel.GetPathName, Len(swModel.GetPathName) - 6) & "PDF"
                                         

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

                                       

                                      End Sub