Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Creating or editting properties with a macro

Status
Not open for further replies.

nickjb

Mechanical
Jun 19, 2007
35
I have a written a macro to populate the properties table for new parts, I also want to use it to update the table for older parts that weren't created with the correct template.

I'm new to macros and vb so I need something simple. Using some borrowed code from this site I've manage to get something working:

Success = swModel.AddCustomInfo3("", "Finish", swCustomInfoText, "")
swModel.CustomInfo2("", "Finish") = "Natural"

This will populate the property 'finish' with the value 'natural' (our default) but if the property has already been set to something else then it overwrites it. I don't want that to happen I want it to keep the current value.


 
Replies continue below

Recommended for you

I've just reread that and it might not be that clear. I need it to:

1. if the property doesn't exist, create it and populate it with a set value
2. if the property exists then keep the current the value
 
SW API Help said:
Description

This property gets and sets the file custom information for the SolidWorks document.



Syntax (OLE Automation)

value = ModelDoc2.CustomInfo2 (configuration, fieldName) (VB Get property)

ModelDoc2. CustomInfo2(configuration, fieldName) = value (VB Set property)



value = ModelDoc2.GetCustomInfo2(configuration, fieldName) (C++ Get property)

ModelDoc2.SetCustomInfo2(configuration, fieldName, value) (C++ Set property)



Input:
(BSTR) configuration
Name of the configuration (see Remarks)

Input:
(BSTR) fieldName
Name of field

Property:
(BSTR) value
Text in the field

If value in the "get" property exist and <> "" then do not change it else delete it and add it with the new value.

SW API Help said:
This method is obsolete and has been superseded by ModelDocExtension::CustomPropertyManager.
Thing get a little bit more complicated with CustomPropertyManager.
 

Get rid of your second line. The AddCustomInfo3 function will not overwrite an existing property. You can verify this by checking the value of the variable "Success" with a message box. All you need to do to get your desired behavior is put "Natural" in your AddCustomInfo3 line and get rid of the next line. So you'll have:
Code:
Success = swModel.AddCustomInfo3("", "Finish", swCustomInfoText, "Natural")

-handleman, CSWP (The new, easy test)
 
Thanks Handleman, that's got it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor