8 Replies Latest reply: Jul 14, 2014 12:58 PM by Daniel Driscoll

    Help with SaveAs in macro

    Daniel Driscoll

      I'm having some trouple getting parts to save as an edrawing format. I've stepped through the program, everything is set to the expected values for all statements, but for some reason the SaveAs function returns false, no errors or warnings, and does not create anything in the target directory. If I use the supposedly obsolete SaveAs3 method (this is how it shows up when recording a macro), it even pops up the edrawings export configuration selection, but does not create a file.

       

       

      Public Function ExportNamedEdraw(swCompModel, ExportPath, sDocNameNoExtension) As Boolean
      
       Dim nErrors As Long
       Dim nWarnings As Long
          NewFilePath = ExportPath & sDocNameNoExtension
          If swCompModel.GetType = swDocPART Then
              NewFilePath = NewFilePath & ".EPRT"
          ElseIf swCompModel.GetType = swDocASSEMBLY Then
              NewFilePath = NewFilePath & ".EASM"
          End If
          
          Set swApp = Application.SldWorks
          bThrowaway = swApp.SetUserPreferenceIntegerValue(swEdrawingsSaveAsSelectionOption, swEdrawingSaveAll)
          ExportNamedEdraw = swCompModel.Extension.SaveAs(NewFilePath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings)
          
      End Function
      

       

       

      Anyone have any ideas as to how to resolve this?

        • Re: Help with SaveAs in macro
          Sean Phillips

          This macro worked for me. I am not sure why you are having issues.

           

          Dim swApp As Object

          Sub main()

           

           

          Set swApp = Application.SldWorks

           

           

          Dim modeldoc As ModelDoc2

           

           

          Set modeldoc = swApp.ActiveDoc

           

           

          ExportNamedEdraw modeldoc, "d:\", "myfile"

           

           

          End Sub

          Public Function ExportNamedEdraw(swCompModel As ModelDoc2, ExportPath, sDocNameNoExtension) As Boolean

           

           

          Dim nErrors As Long

          Dim nWarnings As Long

              NewFilePath = ExportPath & sDocNameNoExtension

              If swCompModel.GetType = swDocPART Then

                  NewFilePath = NewFilePath & ".EPRT"

              ElseIf swCompModel.GetType = swDocASSEMBLY Then

                  NewFilePath = NewFilePath & ".EASM"

              End If

             

              Set swApp = Application.SldWorks

              bThrowaway = swApp.SetUserPreferenceIntegerValue(swEdrawingsSaveAsSelectionOption, swEdrawingSaveAll)

              ExportNamedEdraw = swCompModel.Extension.SaveAs(NewFilePath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings)

             

          End Function

          • Re: Help with SaveAs in macro
            Deepak Gupta

            It would be easy to debug if you can post the complete macro/codes.

              • Re: Help with SaveAs in macro
                Daniel Driscoll

                The full macro is a bit of a mess, but I've gotten Sean's code to run successfully, so it must be related to something else in the code. I've attached the macro below so that you can take a look at it.

                 

                I just tried some more debugging, and I think I've narrowed it down to the following issue:

                When passing a part to this function, I send a variable of type: ModelDoc2/PartDoc but in the function, it shows as a variable of type: Variant/Object/PartDoc

                The error then returns as 512 (swFileSaveAsNoSelection)

                I've updated the declaration to say As ModelDoc2 variable and it populates correctly with the ModelDoc2/PartDoc object as far as I can tell), but this doesn't seem to make a difference.

                The error still returns as 512 (swFileSaveAsNoSelection)

                    • Re: Help with SaveAs in macro
                      Daniel Driscoll

                      This is still throwing an error, but now it is a different error - 256, invalid file extension. I'm not sure how that could be, as the file extensions are correct, as far as I can tell. Does this script work for you?

                       

                      Edit: There was one variable that had been mislabeled, Set ModelDocExt = swModel.Extension should be Set ModelDocExt = swCompModel.Extension

                      This still does not resolve the issue, however, now it gives error 1 - generic save error.

                       

                      Also, changing to that obsolete SaveAs3 method does not work, returning the same generic save error (and if I remove the options, the edrawings configuration selection even shows up, but does not save)

                       

                      I've also just tried this on a coworkers computer with the same failure.

                        • Re: Help with SaveAs in macro
                          Deepak Gupta

                          Works fine for me.

                           

                          If I can know, what is the primary purpose of this macro for you? Only edrawings OR DWG/DXF also. I mean all the option given on the userform or just some of them. I'm asking this because then I can try to simplify the macro.

                            • Re: Help with SaveAs in macro
                              Daniel Driscoll

                              This macro really probably should be a plugin of some sort, I imagine, with all that is going into it, but it is basically a batch process for our sheet metal assemblies. Where I work, we generate a lot of sheet metal assemblies, and the DXFs and eDrawings are used in our shop for reference. Exporting all of these individually is tedious and error prone, so I've been working on this intermittently.

                            • Re: Help with SaveAs in macro
                              Daniel Driscoll

                              I tried changing the extension from .EPRT to .SLDPRT, and it saved the solidworks files. It also works when saving things as .igs files. I don't know how this helps me solve my problem (since I need eDrawings formats...), but maybe it is useful information.