Macro for addding custom properties
Macro for addding custom properties
I have a macro that I use to add custom properties. We just upgraded to SW 2023 and it does not work on files created from the 2023 templates (I made new part/assembly templates from the standard 2023 ones since there was an issue in my old templates relating to unfolding sheet metal parts). Is there some way to see what is wrong with the macro when it runs? It doesn't show any errors, just finishes but the custom properties are not created.
Re: Macro for addding custom properties
You can set a breakpoint at the beginning of the file and step through it line by line to see where it is failing.
open the editor Browse to your file and open.
Next to where it says "Sub main()" click in the little column to the left. A red dot should appear (This is a breakpoint.) The code will stop when it hits this line. (You can have as many setpoints as you like.)
In the debugger, hit the "run" button. You will see a yellow highlight on the line with the break point.
From here, you can hit F-8 and it will step through the code line by line, to try and figure out what the problem is.
open the editor Browse to your file and open.
Next to where it says "Sub main()" click in the little column to the left. A red dot should appear (This is a breakpoint.) The code will stop when it hits this line. (You can have as many setpoints as you like.)
In the debugger, hit the "run" button. You will see a yellow highlight on the line with the break point.
From here, you can hit F-8 and it will step through the code line by line, to try and figure out what the problem is.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
Re: Macro for addding custom properties
The AddCustomInfo function has been marked as obsolete for a LONG time. It's possible they have finally disabled it entirely. You should be using ICustomPropertyManager::Add3. You'll have to use ModelDocExtension to get the CustomerPropertyManager object. Something like this:
Code: Select all
' ******************************************************************************
' C:\Users\timt.MALISH\AppData\Local\Temp\swx10492\Macro1.swb - macro recorded on 05/11/17 by timt
' ******************************************************************************
Dim swApp As Object
Dim Part As ModelDoc2
'Dim cusPropMgr As SldWorks.CustomPropertyManager
'Dim boolstatus As Boolean
'Dim longstatus As Long, longwarnings As Long
Dim mExt As ModelDocExtension
Dim propMgr As CustomPropertyManager
'Dim ActiveConfig As Configuration
Sub main()
Set swApp = Application.SldWorks
'Set swdoc = swApp.ActiveDoc
Set Part = swApp.ActiveDoc
Set mExt = Part.Extension
'Set swConfigMgr = swdoc.ConfigurationManager
'Set ActiveConfig = swConfigMgr.ActiveConfiguration
Set propMgr = mExt.CustomPropertyManager("")
'Dim Configuration As String
'Dim FieldName As String
'Dim FieldValue As String
Dim value As Boolean
'ActConfigName = ActiveConfig.Name
'Replace <PropertyName with what you want your custom property to be called.
'Replace <PropertyType> with the type of property, the drop down in the property manager window. Most often Text will work for anything, but you can set it to match data type if needed/wanted.
'Replace <PropertyValue> with what you want the custom property to be. I usually have it place a - in the field, as older versions of SW would not allow a blank property.
'Copy the value = ... line as many times as needed and update.
value = propMgr.Add3("Revision", swCustomInfoType_e.swCustomInfoText, "--", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Material", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("MaterialNum", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Description", swCustomInfoType_e.swCustomInfoText, " ", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Description2", swCustomInfoType_e.swCustomInfoText, "------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("AltDescription", swCustomInfoType_e.swCustomInfoText, "------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Vendor", swCustomInfoType_e.swCustomInfoText, "None", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("VendorNumber", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Date", swCustomInfoType_e.swCustomInfoText, "XX/XX/XX", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Finish", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Color", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Concentricity", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Parallelism", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Flatness", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Author", swCustomInfoType_e.swCustomInfoText, "TJT", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Number", swCustomInfoType_e.swCustomInfoText, "--------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
'Set ActiveConfig = Nothing
'Set swConfigMgr = Nothing
'Set swdoc = Nothing
'Set swApp = Nothing
End Sub
Re: Macro for addding custom properties
Thanks, I will give that a try. I figured something had changed. I have used that Macro for years now and my templates were old as well, so it always worked until I just tried it on the new templates I created yesterday. I think the templates I was using were from SW2009!JSculley wrote: ↑Wed Aug 16, 2023 8:49 am The AddCustomInfo function has been marked as obsolete for a LONG time. It's possible they have finally disabled it entirely. You should be using ICustomPropertyManager::Add3. You'll have to use ModelDocExtension to get the CustomerPropertyManager object. Something like this:
Code: Select all
' ****************************************************************************** ' C:\Users\timt.MALISH\AppData\Local\Temp\swx10492\Macro1.swb - macro recorded on 05/11/17 by timt ' ****************************************************************************** Dim swApp As Object Dim Part As ModelDoc2 'Dim cusPropMgr As SldWorks.CustomPropertyManager 'Dim boolstatus As Boolean 'Dim longstatus As Long, longwarnings As Long Dim mExt As ModelDocExtension Dim propMgr As CustomPropertyManager 'Dim ActiveConfig As Configuration Sub main() Set swApp = Application.SldWorks 'Set swdoc = swApp.ActiveDoc Set Part = swApp.ActiveDoc Set mExt = Part.Extension 'Set swConfigMgr = swdoc.ConfigurationManager 'Set ActiveConfig = swConfigMgr.ActiveConfiguration Set propMgr = mExt.CustomPropertyManager("") 'Dim Configuration As String 'Dim FieldName As String 'Dim FieldValue As String Dim value As Boolean 'ActConfigName = ActiveConfig.Name 'Replace <PropertyName with what you want your custom property to be called. 'Replace <PropertyType> with the type of property, the drop down in the property manager window. Most often Text will work for anything, but you can set it to match data type if needed/wanted. 'Replace <PropertyValue> with what you want the custom property to be. I usually have it place a - in the field, as older versions of SW would not allow a blank property. 'Copy the value = ... line as many times as needed and update. value = propMgr.Add3("Revision", swCustomInfoType_e.swCustomInfoText, "--", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Material", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("MaterialNum", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Description", swCustomInfoType_e.swCustomInfoText, " ", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Description2", swCustomInfoType_e.swCustomInfoText, "------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("AltDescription", swCustomInfoType_e.swCustomInfoText, "------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Vendor", swCustomInfoType_e.swCustomInfoText, "None", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("VendorNumber", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Date", swCustomInfoType_e.swCustomInfoText, "XX/XX/XX", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Finish", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Color", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Concentricity", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Parallelism", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Flatness", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Author", swCustomInfoType_e.swCustomInfoText, "TJT", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) value = propMgr.Add3("Number", swCustomInfoType_e.swCustomInfoText, "--------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) 'Set ActiveConfig = Nothing 'Set swConfigMgr = Nothing 'Set swdoc = Nothing 'Set swApp = Nothing End Sub
-
- Posts: 194
- Joined: Fri Mar 19, 2021 12:21 pm
- x 37
- x 95
Re: Macro for addding custom properties
Thanks TTevolve, JScully, and SPerman. That macro is going to be very useful to me.
Re: Macro for addding custom properties
I don't know what you want the macro to do, but our templates have no custom properties and we use the custom properties tab and the property tab builder to control the addition of custom properties. These are customizable and will probably do what you want.
Re: Macro for addding custom properties
Pretty much the same thing without the buttons and/or drop down boxes.
The macro comes in handy on imported parts or old parts that were created without the custom properties. Run the macro and you have all your custom properties in the part or assembly.
The macro comes in handy on imported parts or old parts that were created without the custom properties. Run the macro and you have all your custom properties in the part or assembly.
Re: Macro for addding custom properties
Opening the custom properties tab does exactly that.