11 Replies Latest reply on Nov 11, 2018 2:31 PM by Marko Topalovic

    Export each sheet macro

    Marko Topalovic

      Hi,

       

      I have been using a macro I found in this forum to export each sheet as a separate file. This macro is working fine but the "problem" we have when we are using this macro is that filename is generated as "filename - sheetname.extension" and for our needs we have to save the files as "sheetname - filename.extension". the reason behind this is that we have to place SAP code in front of everything to be visible.

      I have tried to modify the macrto for our needs but I am amateur in programming and I can not find the line in code where I can change the order of the generated file name.

      Can someone please tell me how can I modify this code? Also, can I implement custom property in this code?

       

      code:

       

       

      Option Explicit

       

       

      Dim swApp               As SldWorks.SldWorks

      Dim swModel             As SldWorks.ModelDoc2

      Dim swModelDocExt       As SldWorks.ModelDocExtension

      Dim swExportPDFData     As SldWorks.ExportPdfData

      Dim swDraw              As SldWorks.DrawingDoc

      Dim swSheet             As SldWorks.Sheet

      Dim boolstatus          As Boolean

      Dim sFilename           As String

      Dim lErrors             As Long

      Dim lWarnings           As Long

      Dim strSheetName(20)    As String

      Dim varSheetName        As Variant

      Dim varSheetCount       As Variant

      Dim i                   As Long

       

       

      Private Sub cboFileExt_Change()

       

       

      End Sub

       

       

      Private Sub cmdSave_Click()

       

       

      On Error GoTo error

       

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      Set swDraw = swApp.ActiveDoc

       

      If swModel.gettype <> 3 Then

          MsgBox "Open Document is not a Drawing file.", vbCritical, "Wrong Document Type Open"

      End If

       

       

      Set swModelDocExt = swModel.Extension

      Set swExportPDFData = swApp.GetExportFileData(1)

       

       

      varSheetCount = swModel.GetSheetCount()

       

       

      For i = 1 To varSheetCount

          swDraw.SheetPrevious

      Next i

       

       

      For i = 0 To varSheetCount - 1

          If i <> 0 Then

              swDraw.SheetNext

          End If

          Set swSheet = swDraw.GetCurrentSheet

          strSheetName(i) = swSheet.GetName

          Set swSheet = Nothing

         

          varSheetName = strSheetName(i)

         

          sFilename = swModel.GetTitle & "." & frmMain.cboFileExt.Text

       

       

          If swExportPDFData Is Nothing Then MsgBox "Nothing"

          boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varSheetName)

         

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

      Next i

       

       

      Unload frmMain

       

       

      Exit Sub

       

       

      error:

      MsgBox "An error has occured - Please close and reopen SolidWorks to try again", vbCritical, "Program Error"

      Exit Sub

       

       

      End Sub

        • Re: Export each sheet macro
          Deepak Gupta

          You need to change this line

          sFilename = swModel.GetTitle & "." & frmMain.cboFileExt.Text

          to

           

             sFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

             sFileName = Left(sFileName, InStrRev(sFileName, ".") - 1)

             sFileName = strSheetName(i)  & "-" & sFileName & "." & frmMain.cboFileExt.Text

            • Re: Export each sheet macro
              Marko Topalovic

              Thank you very much, it works fine!

              • Re: Export each sheet macro
                Marko Topalovic

                Hello again,

                 

                the macro works fine, but I have found that it has some problems with save location. For example, when I am working on a part and then start working on a another part that is not in the same file location as the first part, macro will export to the location of the first part.

                Can you help me with this? I have seen that it is possible to automatically create a folder when exporting, can this be incorporated within this macro?

                 

                And, can you help me with the name modification?

                instead of strSheetName I would like to incorporate custom property from the model

                sFileName = strSheetName(i)  & "-" & sFileName & "." & frmMain.cboFileExt.Text

                  • Re: Export each sheet macro
                    Deepak Gupta

                    Marko Topalovic wrote:

                     

                    For example, when I am working on a part and then start working on a another part that is not in the same file location as the first part, macro will export to the location of the first part.

                    This would be interesting to see. Can you share your screen to debug this?

                     

                    I have seen that it is possible to automatically create a folder when exporting, can this be incorporated within this macro?

                     

                    And, can you help me with the name modification?

                    instead of strSheetName I would like to incorporate custom property from the model

                    sFileName = strSheetName(i) & "-" & sFileName & "." & frmMain.cboFileExt.Text

                    Yes both of these can be done.

                      • Re: Export each sheet macro
                        Marko Topalovic

                        Hi,

                        Deepak Gupta wrote:

                         

                        This would be interesting to see. Can you share your screen to debug this?

                         

                        Please find a link to my video attached.

                         

                        02-11-2018-07-13-13.mp4 - Google Drive

                         

                        You can see one file located in the desktop, and one file located on the server. First, I opened the file on the file located in the server and saved it, then I opend a file located on the desktop and tried to export it with this macro. After exporting you can see that exported files didn't appear on the desktop.

                        Deepak Gupta wrote:

                         

                        Yes both of these can be done.

                        Could you please show me hot to implement both of this codes in the macro that is written at the beginning.

                        If not a problem, please add a comment next to a line of code where you implement this code so I could learn a bit.

                         

                        Thank you

                          • Re: Export each sheet macro
                            Deepak Gupta

                            Marko Topalovic wrote:

                             

                            You can see one file located in the desktop, and one file located on the server. First, I opened the file on the file located in the server and saved it, then I open a file located on the desktop and tried to export it with this macro. After exporting you can see that exported files didn't appear on the desktop.

                            So where the exported file is getting saved? With the video it might be little difficult to debug the issue, so can you can do a screen share over team viewer?

                              • Re: Export each sheet macro
                                Marko Topalovic

                                I am sorry ,I am not allowed to share my screen with anyone.

                                The file sometimes saves in the location of the part that is saved first, and sometimes I can not find it anywhere.

                                 

                                As I have seen it in the code, and that is what makes me confused, name of the file is generated with path of the file. I tried to find how to add a line of code where exact location will be defined, that would solve the problem.

                                As, I said I am just a begginer in macros, and a lot of things may not have sense when I write them, so sorry about that.