This macro worked for me. I am not sure why you are having issues.
Dim swApp As Object
Set swApp = Application.SldWorks
Dim modeldoc As ModelDoc2
Set modeldoc = swApp.ActiveDoc
ExportNamedEdraw modeldoc, "d:\", "myfile"
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"
Set swApp = Application.SldWorks
bThrowaway = swApp.SetUserPreferenceIntegerValue(swEdrawingsSaveAsSelectionOption, swEdrawingSaveAll)
ExportNamedEdraw = swCompModel.Extension.SaveAs(NewFilePath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings)
It would be easy to debug if you can post the complete macro/codes.
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)
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.
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.
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.
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.