Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Macro to save with File Properties 1

Status
Not open for further replies.

bri011

Electrical
Sep 11, 2006
20
0
0
US
I swear that somewhere I saw a macro that saves a part/assembly based off of some of the part properties (or writes the file name into part properties if they don't exist) on the first save. Am I wrong?

Commonly used properties might be part number and description; I'd like to use it to consistently name my files and populate some of the properties in one operation.

If it doesn't exist, I bet it would be very easy for somebody knowledgeable with VBA to write something. In fact, I've laid out the dialog for it, but I don't know VBA well enough to write the code.

Do any of you know of such a program? Or would any of you be interested in writing something if I laid out the functionality? How about for free & to share with the community? It can't hurt to ask.
 
Replies continue below

Recommended for you


The following code was posted by SolidAir in the link above.
Apparently it worked in a previous version of SW. Does anything need to be changed in the macro to make it work in SW07? Or is it a setting on my computer that needs to be changed? I have all the SW07 libraries referenced.
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()

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

[cheers]
 
CorBlimeyLimey,

You never cease to amaze me on how you can remember and/or find these old threads. I barely remember writing the code.

Amazing too is I just installed SW2007 SP1.1 on my home computer last night so I could try the code myself.

I looked through the API help and found that the CustomInfo2 and SaveAs4 calls are now obsolete. However, the code still ran for me. So now I have some questions for you.

1) What SP of SW2007 are you running?

2) In the VBA editor, what boxes do you have checked in the References dialog (Tools, References)?

3) What error are you getting?

4) Did you replace "ConfigurationName" with the actual configuration name in your model?

5) Do you have a "SalesOrder" and "Unit" custom property?

6) If yes, what were their values?

Please excuse the number of questions, it is the only way I know how to troubleshoot this one over the internet.

If I get a chance after Thanksgiving dinner today, I will try to re-write the code to bring it up to 2007.

SA
 
Doh ... I assumed that the "configuration name" took whatever was active. I changed it to "Default" and the macro now works great.

BTW, there was no error message given. It just didn't work.

Would it be an easy change to have the macro query where the document should be saved to?

Thanks for the help. Hope you enjoy the dinner.

[cheers]
 
CorBlimeyLimey,

Dinner was better than expected. Thanks!

The original code was written as an example only. It is pretty basic. It does not contain any error handling (which is the reason why you did not get any error messages; none of the calls produce an error message unless an object is invalid).

I suppose it could be written to query for the file location. I am not exactly sure how I would handle it; maybe a browse to folder routine. But as I said before, I wrote it as an example. If I were to write it as an actual application, I would have handled the configuration name differently, added a check to make sure the two custom properties existed and, checked the retval, SaveErrors and SaveWarnings variables for errors.

If you really would like to see some code for saving the file to a particular folder, let me know. I will see if I can come up with something.

Below is my revised code using up-to-date API calls. I am not real impressed with them though. More code is required (although I could have combined calls) and I had to declare a few more variables.

Code:
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelExt As SldWorks.ModelDocExtension
Dim swCustPropMgr As SldWorks.CustomPropertyManager


Dim FileName As String
Dim SaveErrors As Long
Dim SaveWarnings As Long
Dim retval As Boolean
Dim SalesOrderNumber As String
Dim UnitNumber As String
Dim PropertyValue 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 model doc extension object[/color]
    Set swModelExt = swModel.Extension
    
    [COLOR=green]'get custom property manager object[/color]
    Set swCustPropMgr = swModelExt.CustomPropertyManager("Default")
    
    [COLOR=green]'get sales order #[/color]
    swCustPropMgr.Get2 "SalesOrder", PropertyValue, SalesOrderNumber
    
    [COLOR=green]'get unit #[/color]
    swCustPropMgr.Get2 "Unit", PropertyValue, UnitNumber
    
    [COLOR=green]'create file name[/color]
    FileName = SalesOrderNumber & UnitNumber & ".sldprt"
    
    [COLOR=green]'save part with new file name[/color]
    retval = swModelExt.SaveAs(FileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, SaveErrors, SaveWarnings)
    
End Sub

SA
 
SolidAir ...

Having the ability to browse to a folder to save in would be nice, but (for me) it is not critical or urgent. The OP may have more of a need though.

Also, could a space between the fields be incorporated in the file name? Typing a space at the end of the "SalesOrder" property or beginning of the "Unit" property or having a separate "space" property would do the trick, but is there API code to do that?

I know I need to make time to learn this API stuff ... but the few working brain cells I have left are fading fast ... especially the short-term-memory ones. [sad]

Thanks again for your help.

[cheers]
 
That's nice... a step in the right direction for me.

I would need to be able to browse to a folder. I'd use this for the first file save; after that it wouldn't be necessary (as the file is already named properly).

Thanks for the response...

Brian
 
CorBlimeyLimey and bri011,

Follow link to download my updated macro. I added a browse to folder routine, some error handling and had the program put a space between the fields in the file name. By no means is it a polished application. Please read the "this program requires section". You must add the Microsoft Scripting Runtime to the references in order for the macro to run correctly.

To give credit where credit is do, the Browse for folder routine was written by Stephen Fonnesbeck. I did modify it to add the ability for the user to manually enter a folder path. This code came from some other code I saw somewhere but do not remember who to give credit to.

SA
 
OK, the new macro is now working great.

However, when I created a New Macro Button, using the Customize > Commands > Macro function, the macro would not run ... but it would run from within the SW/MS-VB editor. ????

Anyway, I found that the New Macro Button Method field was automatically populated as CreateFileNameFromProp1.ClearObjects but needed to be changed to CreateFileNameFromProp1.main and then all was well.

The browse function is a great help.

Many thanks for your time and effort.

[cheers]
 
CorBlimeyLimey,

Sorry about that, I had to move the ClearObjects procedure in front of the sub main because the macro would not run from the run macro command. This is just one of the quirks with VBA in SolidWorks.

SA
 
Status
Not open for further replies.
Back
Top