Save as DWG macro

Jan 16, 2008 8 Replies

We have been using a macro to save a drawing as a PDF that was last enhanced by Matthew Lorono. This works pretty well in that if the save fails, you get errors returned, such as the file was not writable, maybe because it was read-only.



So, I said, since this one is so handy, why not make another one-button push to save out the DWG copy also. Great - change the PDFs to DWGs. Works. Mostly. It will save the file properly, but if the file already exists, and it's read-only, then there still are no errors returned. It's not that I am not seeing the returns, the code is telling me that the "MB = Model.SaveAs4(MyPathConf & "\" & NewName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Errs, Warnings)" returns true.



Any thoughts as to why?



WT


WT,

Have the code do a search for the file's existence before executing the save code. This will allow you to not rely on errors to take action, but to allow the code to take the appropriate action before errors are caused.

Matt Lorono

formatting link

That makes some sense, so how do I query it to determine if it's open by something else, read-only, etc.?

But, still on the original question, why would the PDF save properly return errors, and the DWG save not return those same errors?

WT

WT,

It may actually be a bug. Have you tried the operation manually to see if you get the same results? If so, bug. If not, then maybe it's something to do with how the code handles the errors. If it is a bug, you'll still need a work-around, but it should be reported to S/W through your VAR.

As far as having the code check to see if the file exists, try checking out MS API help first to see if there is an example already in there for this kind of action.

Matt Lorono

formatting link

Manually works properly. If the file is read-only it comes back with the proper notification. With the API SaveAs4, nothing comes back as a problem.

WT

Wayne,

Running SW2007 sp0.0 here.

I experimented with both SaveAs PDF and SaveAs DWG, and neither one prompted an error message for me...no matter what the new file was saved.

I'm guessing the value at the "swSaveAsOptions_Silent" position is supposed to take care of this. Looking at the SW API Help for SaveAs4, I found this disclaimer on the bottom of the swSaveAsOptions_e page: "These options only apply to saving to native SolidWorks file formats."

To answer your question posted above...here you go:

If (Dir(MyPathConf & "\" & NewName) = "") Then 'Check if File Doesn't Already Exist. MB = Model.SaveAs4(strAcadName, swSaveAsCurrentVersion, 0, Errs, Warnings) If (MB) Then 'Nothing. Do it this way because looking for "(Not(MB))" is unreliable. Else MsgBox "File was not Created.", vbCritical, "SaveAsDWG" End If Else MsgBox "File Already Exists.", vbCritical, "SaveAsDWG" End If

Later, Ken

Also keep in mind that if someone else has the file open you wont be able to overwrite it, even when the file property wont way its read only. We have a similar problem with our PDF saving system.

Also our VAR claims that there are some API errors concerning save as dxf, making it not save each time, sometimes it just skips it for whatever reason.

We are on 2007 SP5 and I had seen the message about the options only applying to SW natives, but the PDF save does work properly for me. If the file is read-only by the attribute, or if an app has it open, it will return the proper errors. The fact that there is a recognized bug about saving to DXF probably means that same thing applies for DWG.

WT

WT and Ken,

If you have a working version of your new macro with the addition of the file name check, I'd be interested in hosting on my site, with new edit credits going to you and Ken (Tin Man). Let me know.

Matt Lorono

formatting link

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required