Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro for saving file using Custom Properties as file name 3

Status
Not open for further replies.

JoeMoss

Mechanical
Mar 8, 2006
37
Alright, I am new to Macros so I have just been recording and reading at this point. My goal is to create a macro that will combine two or more fields from the custom properties as the file name and save in the current directory. Is there a macro out that does something similar to this that I can look at?

Thanks!
Joe
 
Replies continue below

Recommended for you

JoeMoss,

Just so I understand what you want. Yow want a macro to take the custom property name or the value for the custom property, combine them and then save the current solidworks document using those custom properties as the file name?

SA
 
SA, you are correct. We are importing data into a DT, part of that data is sales order # and a unit #. I am assigning those values to custom properties (for drawings and file name) and I would like to pull from custom properties to save off the new file.

New file name would be <sales order #><Unit #>.sldprt

 
JoeMoss,

Since you are trying to learn about macros, here is just a code snippet of how you might go about it. The macro will run but it does not include any error checking that you would want to add to make it a more polished macro and I do not include any code on how to obtain the configuration name.

Code:
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Dim FileName As String
Dim SaveErrors As Long
Dim SaveWarnings As Long
Dim retval As Boolean
Dim SalesOrderNumber As String
Dim UnitNumber As String



Sub main()

    [COLOR=green]'get SolidWorks object[/color]
    Set swApp = GetObject(, "SldWorks.Application")
    
    [COLOR=green]'get active model object[/color]
    Set swModel = swApp.ActiveDoc
    
    [COLOR=green]'get sales order #[/color]
    SalesOrderNumber = swModel.CustomInfo2("[i]ConfigurationName[/i]", "SalesOrder")
    
    [COLOR=green]'get unit #[/color]
    UnitNumber = swModel.CustomInfo2("[i]ConfigurationName[/i]", "Unit")
    
    [COLOR=green]'create file name[/color]
    FileName = SalesOrderNumber & UnitNumber & ".sldprt"
    
    [COLOR=green]'save part with new file name[/color]
    retval = swModel.SaveAs4(FileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, SaveErrors, SaveWarnings)
    
End Sub

One question I forgot to ask was if the custom properties are in the Custom or Configuration Specific tab. If they are in the Custom tab, replace "ConfigurationName" with "".

Just so you know, I did test the code, and it did work on my computer so if you have any problems running it, get back to me and I will try and help you work through it.

SA
 
SA,
Thanks for the code, it works perfectly. I was able to change a couple of the names and now it will save it with the correct name!

I am slowing getting this, thanks for your help!

Joe

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor