Save new part to path

I've tried quite a few combinations of the API for saving a new part to a specified path.  I can't seem to get it to work other than saving a "copy"... it saves the file, but leaves the active document as "Part1".  Can anyone provide a sample of how this should be done?

SolidworksApi macros
Answers 22
FV
FV 2012-03-26 Best Answer

Hi Paul, post what code you've got until now and we'll correct accordingly.

Also, try and explain exactly what you expect this code to do.

For example, do you expect this part close automatically once it's been saved?

Cheers

PM
PM 2012-03-27

Well, here is where I last left it trying to get it to work.  The expected out come is that I show the userform, ask for the user to put in a standard number and the code will determine the path based on the number they enter, and save the Part file to the path.  No closing, no making an assembly, just save to the pre-determined path and name.

<p>Dim swApp As Object<br>Dim Part As Object<br>Dim boolstatus As Boolean<br>Dim longstatus As Long, longwarnings As Long</p><p><br>Sub SaveTool()<br>Set swApp = Application.SldWorks<br>Set Part = swApp.ActiveDoc<br>    Tool = UCase(saveastoolform.mytoolnumber)<br>    saveastoolform.Hide<br>    'MsgBox "Tool: " & Tool<br>    ToolType = Left(Tool, 2)<br>    'MsgBox "ToolType: " & ToolType<br>    ToolThousand = Mid(Tool, InStr(1, Tool, "-") + 1, 2) 'returns 2 numbers after the "-"<br>    'MsgBox "ToolThousand: " & ToolThousand</p><p>    Select Case ToolType 'get 40, 41, 50, 51, EXP<br>        Case "40"<br>            toolpath = "H:\Working 40 #'s\" & ToolType & "-" & ToolThousand & "000 - " & ToolThousand & "999\"<br>        Case "41"<br>            toolpath = "H:\Working 41 #'s\"<br>        Case "50"<br>            toolpath = "H:\Working 50 #'s\" & ToolType & "-" & ToolThousand & "000 - " & ToolThousand & "999\"<br>        Case "51"<br>            toolpath = "H:\Working 51 #'s\"<br>        Case "EX"<br>            toolpath = "G:\EXP\EXP'S " & ToolThousand & "000-" & ToolThousand & "999\"<br>            If Left(ToolThousand, 1) = 0 Then<br>                toolpath = toolpath & Mid(Tool, InStr(1, Tool, "-") + 2, 4) & "\" & Tool & ".sldprt"<br>            Else<br>                toolpath = toolpath & Mid(Tool, InStr(1, Tool, "-") + 1, 5) & "\" & Tool & ".sldprt"<br>            End If<br>        Case Else<br>            If ErrorMsg = True Then<br>                ToolsNotFound = ToolsNotFound & ", " & Tool<br>            Else<br>                ToolsNotFound = Tool<br>                ErrorMsg = True<br>            End If<br>    End Select<br>    If ToolType <> "EX" Then<br>        toolpath = toolpath & Tool & "\" & Tool & ".SLDPRT"<br>    End If<br>longstatus = Part.saveas3(toolpath, 0, 2)</p><p><br>End Sub</p>
JS
JS 2012-03-27

According to the API docs, SaveAs3 is obsolete, and has been repelaced by ModelDocExtension::SaveAs.  Perhaps you will have better results with the repalcement method.

Jim S.

FV
FV 2012-03-27

boolstatus = Part.Extension.SaveAs(toolpath, 0, 0, nothing, 0, 0)

SF
SF 2012-03-27

try changing your save path.  You have as a save path "H:\Working 41 #'s".  The # in the path is an invalid Character.

FV
FV 2012-03-27

Maynard, # is allowed...

For windows file systems:

<p><span style="font-family: 'Segoe UI', Arial, Verdana, Tahoma, sans-serif; font-size: 11px; background-color: #ffffff;">A filename cannot contain any of the following characters:</span></p><p class="indent" style="padding-right: 20px; padding-bottom: 10px; padding-left: 20px; font-family: 'Segoe UI', Arial, Verdana, Tahoma, sans-serif; font-size: 11px; background-color: #ffffff;">\ / : * ? " < > |</p>

Filipe

SF
SF 2012-03-27

Ok, not sure what happend here, but if i replied twice, I'm sorry.

Filipe you are correct, I just tend to stay away from special characters all together.

I have used Saveas4 for years without any troubles.

In looking at your saveas3 line, your argument for the saveasoption is 2.  If I read the list of saveasoption_e correctly, i think that you are trying to save the part as a detatched drawing.  i would reccomend either 0(Avoid rebuild on Save) or 7(Silent).

FV
FV 2012-03-27

Maynard, I'm 100% with you on that one though.

I always recommend people not to use special characters. I even NameMyFileWithNoSpacesAndCapitals.

Cheers

PM
PM 2012-03-27

I get a run-time error '13' type missmatch on the SaveAs line.  Ideas?

PM
PM 2012-03-27

Agreed about the naming, but that was done before my time... and before SolidWorks.

FV
FV 2012-03-27

Instead of Nothing try Null...

PM
PM 2012-03-27

nope, same error

FV
FV 2012-03-27

How about...

set boolstatus = Part.Extension.SaveAs(toolpath, 0, 0, nothing, 0, 0)

Filipe

PM
PM 2012-03-27

"compile error: object required"

FV
FV 2012-03-27

Make sure the object Part is something.

Msgbox NOT Part is Nothing 'Should return TRUE

PM
PM 2012-03-27

and I do get a MsgBox that says "True"

FV
FV 2012-03-27

Do the same for your toolpath variable now.

The line boolstatus = Part.Extension.SaveAs(toolpath, 0, 0, nothing, 0, 0) should work (works on my system).

You can also try Call Part.Extension.SaveAs(toolpath, 0, 0, nothing, 0, 0) .

Filipe

PM
PM 2012-03-27

can you post your entire code here?  I can see my "toolpath" is correct in the locals window.

FV
FV 2012-03-27

Paul, I just did a simple test, your code wasn't complete (Tool = UCase(saveastoolform.mytoolnumber) ).

Here's what I got:


<p>Sub main()</p><p></p><p>Dim swApp As SldWorks.SldWorks</p><p>Dim Part As ModelDoc2</p><p></p><p>Set swApp = Application.SldWorks</p><p>Set Part = swApp.ActiveDoc</p><p></p><p>Dim error As Long, warning As Long</p><p>Call Part.Extension.SaveAs(Part.GetPathName & "_#TEST.SLDPRT", 0, 0, Nothing, error, warning)</p><p></p><p>MsgBox error & vbCrLf & warning</p><p></p><p>End Sub</p><p></p>

Note: Try hitting the stop (square) button, before hitting the "play" (triangle) button.

Filipe

FV
FV 2012-03-27

Ok, I think I got it...before your Select statement, define your toolpath variable like so:

Dim toolpath as String

Filipe

PM
PM 2012-03-27

ok, well it works now... something is different, but all I did was copy your code, put my string for toolpath in before and replace your "......TEST.SLDPRT" parameter with "toolpath" and it worked.

Thanks Filipe

FV
FV 2012-03-27

Hmm...that's weird. Maybe you had an extra space character in there somewher.

VBA is pretty picky and isn't always that helpful with errors like that.

Anyway, glad it works.

Please mark whichever reply was most helpful, in order to close the discussion and help whoever may need to know the answer.

Cheers

Filipe