19 Replies Latest reply on Jan 11, 2018 8:14 AM by Timo Gulbinas

    Save drawing to PDF with date

    Zac Evans

      I have a macro that saves the current drawing to pdf in the same file location and the name of the drawing model, I'm new to macros and can't figure out how to have it save today's date to the end of the name (eg. 1234-567-8910 (3-23-16)) in the m-dd-yy format. here's the code I have for it.

       

      Option Explicit

       

      Dim FileName As String

      Dim swExportPDFData As SldWorks.ExportPdfData

      Dim swModel As SldWorks.ModelDoc2

      Dim swApp As SldWorks.SldWorks

       

      Sub main()

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

       

      'Save Drawing

      swModel.Save3 0, 0, 0

       

      'Checks to make sure current file is a drawing

      If swModel.GetType = swDocDRAWING Then

          'Gets current file name and path

          FileName = swModel.GetPathName

          'strips solidworks extension off name and replaces with pdf

          FileName = Left(FileName, Len(FileName) - 6) & "pdf"

          'message box line used to confirm modified file name

          'MsgBox FileName

          'sets current pdf export options

          Set swExportPDFData = swApp.GetExportFileData(1)

          'saves drawing as pdf

          swModel.Extension.SaveAs FileName, 0, 0, swExportPDFData, 0, 0

      Else

          MsgBox "Current File is not a Drawing"

          End

      End If

       

       

      End Sub

        • Re: Save drawing to PDF with date
          Viktor Bovzdarenko

          Hi Zac

          The date can be added by just calling 'Date':

                 Dim s As String

                 s = Date

              FileName = Left(FileName, Len(FileName) - 6) & s &"pdf"

           

          Or #TASK  can be used; in  #TASK  you can change date format or modify the order of file name, custom property or date  in newly created PDF file.

            • Re: Save drawing to PDF with date
              Zac Evans

              I put in that code but it doesn't seem to do anything now when I run it. Any idea why?

                • Re: Save drawing to PDF with date
                  Deepak Gupta

                  You need to format the date in required format. So use these lines

                  Dim s As String

                  s = Format(Date, "m-dd-yy")

                  And then change this line

                  FileName = Left(FileName, Len(FileName) - 6) & s & "pdf"

                  to

                  FileName = Left(FileName, Len(FileName) - 7) & " (" & s & ")" & ".pdf"

                    • Re: Save drawing to PDF with date
                      Zac Evans

                      Ok here's what I've got now but it's not doing anything when I run it. I really appreciate your help with this, this will be a huge time saver!

                       

                      Option Explicit

                       

                      Dim FileName As String

                      Dim s As String

                      s = Format(Date, "m-dd-yy")

                      Dim swExportPDFData As SldWorks.ExportPdfData

                      Dim swModel As SldWorks.ModelDoc2

                      Dim swApp As SldWorks.SldWorks

                       

                      Sub main()

                       

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                       

                      'Save Drawing

                      swModel.Save3 0, 0, 0

                       

                      'Checks to make sure current file is a drawing

                      If swModel.GetType = swDocDRAWING Then

                          'Gets current file name and path

                          FileName = swModel.GetPathName

                          'strips solidworks extension off name and replaces with pdf

                          s = Date

                          FileName = Left(FileName, Len(FileName) - 7) & " (" & s & ")" & ".pdf"

                          'message box line used to confirm modified file name

                          'MsgBox FileName

                          'sets current pdf export options

                          Set swExportPDFData = swApp.GetExportFileData(1)

                          'saves drawing as pdf

                          swModel.Extension.SaveAs FileName, 0, 0, swExportPDFData, 0, 0

                      Else

                          MsgBox "Current File is not a Drawing"

                          End

                      End If

                       

                      End Sub

                      • Re: Save drawing to PDF with date
                        Prasanth Kumar

                        i Have a doubt. This program stores the drawing with same name in respective directory. I need to run this code from excel. It shows an error. Could u help me?

                         

                         

                         

                        Option Explicit

                         

                        Dim FileName As String

                        Dim swExportPDFData As SldWorks.ExportPdfData

                        Dim swModel As SldWorks.ModelDoc2

                        Dim swApp As SldWorks.SldWorks

                         

                        Sub main()

                         

                        Set swApp = Application.SldWorks

                        Set swModel = swApp.ActiveDoc

                         

                        'Save Drawing

                        swModel.Save3 0, 0, 0

                         

                        'Checks to make sure current file is a drawing

                        If swModel.GetType = swDocDRAWING Then

                            'Gets current file name and path

                            FileName = swModel.GetPathName

                            'strips solidworks extension off name and replaces with pdf

                            FileName = Left(FileName, Len(FileName) - 6) & "pdf"

                            'message box line used to confirm modified file name

                            'MsgBox FileName

                            'sets current pdf export options

                            Set swExportPDFData = swApp.GetExportFileData(1)

                            'saves drawing as pdf

                            swModel.Extension.SaveAs FileName, 0, 0, swExportPDFData, 0, 0

                        Else

                            MsgBox "Current File is not a Drawing"

                            End

                        End If

                  • Re: Save drawing to PDF with date
                    Sanya Shmidt

                    Dim s as string

                    s = Replace(CStr(Date), "/", "-")

                    ......

                      'saves drawing as pdf

                        swModel.Extension.SaveAs FileName & s, 0, 0, swExportPDFData, 0, 0

                     

                     

                    hope that helps

                    • Re: Save drawing to PDF with date
                      Zac Evans

                      Thank you for your help everyone! With all your help it's now working perfectly! Here is the final version for anyone who would like to use it.

                      • Re: Save drawing to PDF with date
                        Bernie Daraz

                        I wouldn't suggest doing that if the file refers to other uses of this part in other assemblies for instance. It would be the same suggestion if you were to add a revision to the file name. You might find that the 'other' assemblies can no longer 'find' this part.

                        • Re: Save drawing to PDF with date
                          Timo Gulbinas

                          Very nice! Is there a way to run it from Excel? What do I have to chance to run it from excel?

                          • Re: Save drawing to PDF with date
                            Timo Gulbinas

                            We have a sample test report that is in excel and it get´s the dimensions from a SW drawing.

                            This excel vda code will export a PDF from an active SW drawing with a defined file name to C:\

                             

                            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 c_Path              As String

                                Dim c_Name              As String

                                Dim ZNR                 As String

                                Dim TNR                 As String

                                Dim strFile             As String

                                Dim i                   As String

                                Dim lErrors             As Long

                                Dim lWarnings           As Long

                                Dim strSheetName(4)     As String

                                Dim varSheetName        As Variant

                             

                            Sub PDFexportAktiveSWDraw()

                             

                            c_Path = "C:\"

                             

                                Set swApp = CreateObject("SldWorks.Application")

                                swApp.Visible = True

                             

                             

                               Set swModel = swApp.ActiveDoc

                                If swModel Is Nothing Then

                                MsgBox "No model!", vbOKOnly, "WARNING!"

                                Exit Sub

                              End If

                             

                              If swModel.GetType <> 3 Then

                                MsgBox "No drawing!", vbOKOnly, "WARNING!"

                                Exit Sub

                              End If

                             

                                Set swModelDocExt = swModel.Extension

                                Set swExportPDFData = swApp.GetExportFileData(1)

                                swExportPDFData.SetSheets swExportData_ExportCurrentSheet, ""

                             

                                c_Name = "PB"

                                ZNR = swModel.CustomInfo("name1")

                                TNR = swModel.CustomInfo("name2")

                                FileName = c_Name & TNR & "_" & ZNR & "_" & Format(Date, "ddMMyyyy") & ".pdf"

                               

                                If swExportPDFData Is Nothing Then MsgBox "No pdf data to export!"

                                swExportPDFData.ViewPdfAfterSaving = True

                                boolstatus = swModelDocExt.SaveAs(c_Path & FileName, 0, 0, swExportPDFData, lErrors, lWarnings)

                             

                            End Sub