VBA - Update Custom Property Template

Long time reader, first time poster.

Is it possible to use VBA to select the custom property template for a part or assembly? I have a macro that opens all the parts in a folder, uses information from a userform and dissects the "Save As" name to automatically fill out 80% of our custom properties, rebuilds, and then closes the file. If we ever "steal" a design from another job we have done, there is a chance that the custom property template might not be correct for the current job. I want to keep our guys from having to change each individual template from the drop down for each part.

Thanks.

SolidworksApi macros
Answers 10
DH
DH 2018-07-27 Best Answer

Josh,

I understand your question and have faced the same challenge.  To my knowledge,  you could not access the custom property template through the API until 2018.  I have actually implemented a macro just recently that solves this issue. I had some API support that helped me find this new command. I was searching in 2017, but I could not find.

Solution:

1. Make sure you have the 2018 SOLIDWORKS object libraries referenced.

2. Dim and Set objects etc as follows (object names are arbitrary)

3. Implement the 2018 command to change the template file

-------------------

Dim swApp As SldWorks.SldWorks

Dim swModel As ModelDoc2

Dim swModelExtension As ModelDocExtension

--------------------------

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc (or other technique to access the part or assy doc)

Set swModelExtenstion = swModel.Extension

Now you can access  and change the custom property template file.  I found it necessary to set the template to "-None-" and then re-select the desired template. This new selection can be done via custom drop down userform. I created one that loads all our custom property templates in to a drop down for the user to select. The routine then drills through the open assembly via recursion and forces the new template to parts and assemblies per the drop down selection.

The ( ) parameter is to be (True) for weldment parts, and (False) if not.

swModelExtension.CustomPropertyBuilderTemplate(False) = "-None-"

swModelExtension.CustomPropertyBuilderTemplate(False) = "template.prtprp" (your template file name here for parts)

swModelExtension.CustomPropertyBuilderTemplate(False) = "template.asmprp" (your template file name here for assemblies)

Hope this helps. It has been a great solution for us.

Thanks,

comment imagecomment image
DG
DG 2018-07-29

, thanks a lot for sharing this.

NJ
NJ 2019-10-16

Hi Derek and Josh,

Could you guys post this Macro in its entirety?

Thanks,

Neil 

NJ
NJ 2019-10-17

We get in parts from customers and more often than not we'll need to rework them for manufacture and generate our own shop prints. I'm trying to develop a macro that will let me open an assembly and set the custom property template for all the assemblies and parts within. I'm really out of my depth, but figured I'd start with a program that just sets the path for a single part and work out from there.

All my macro experience is limited to editing existing macros, tweaking them till I get what I'm looking for.

Here's what I have thus far:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Dim swApp                             As Object
Dim swModel                          As ModelDoc2
Dim swModelExtension          As ModelDocExtension


Sub main()

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set swModelExtension = swModel.Extension

swModelExtension.CustomPropertyBuilderTemplate(False) = "-None-"


swModelExtension.CustomPropertyBuilderTemplate(False) = "Parts.prtprp"

End Sub

 

I get no errors, but sadly I also get no results.

I'm just trying to open a part and set/correct the custom file properties to our template.

Any thoughts?

I suspect that my issue lies with the fact that this is a property and I might not be using it correctly in the code.

2019 SOLIDWORKS API Help - CustomPropertyBuilderTemplate Property (IModelDocExtension) 

Thanks,

Neil

DG
DG 2019-10-18

Have you checked and made sure that your property builder templates are in the correct location?

NN
NN 2019-10-18

Neil,

I, like you, am a macro tweaker. I boiled down the macro we have to a single line of code. I suppose this is a terrible practice(?), but I think it will work on a single part or assembly as you have requested. What you need to do:

1) Swap out what's in quotes in the code below to your path/file location

2) With a file open, uncomment the required line below and run the macro.

3) This is quirky, but you need to force the update in the Custom Property Tab. I've found the best way to do this is to tab over into another part/assembly and then come back. You may even check to see if this makes your existing code work.

Something else to keep in mind: this won't work on anything older than SW 2018. Let me know if this works for you.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Sub TemplateChange()

'Application.SldWorks.ActiveDoc.GetActiveConfiguration.GetRootComponent3(True).GetModelDoc2.Extension.CustomPropertyBuilderTemplate(False) = "C:\YourAssemblyTemplateP.asmprp"
'Application.SldWorks.ActiveDoc.GetActiveConfiguration.GetRootComponent3(True).GetModelDoc2.Extension.CustomPropertyBuilderTemplate(False) = "C:\YourPartTemplate.prtprp"

End Sub

NN
NN 2019-10-18

Scratch my last post, Neil. Your code worked on my computer. I boiled it down to a single line again, but it worked.

Application.SldWorks.ActiveDoc.Extension.CustomPropertyBuilderTemplate(False) = "C:\YourAssemblyTemplateP.asmprp"

I think you just need to force the change. The only way I know to do that is by changing your active window to another file and then coming back. That should refresh the Custom Property Tab for whatever reason.    ¯\_(ツ)_/¯

NN
NN 2019-10-21

Niel,

This code should iterate through an open assembly and assign templates to parts and assemblies based on what is detected. Not a lot of error handling or validation. It will skip a part if if an error shows up trying to access it. This happened to me when I came across a suppressed part. Hope it helps.

Sub main()

Dim swApp As Object
Dim swModel As ModelDoc2
Dim swModelExtension As ModelDocExtension
Dim i As Integer
Dim vCmps As Variant
Dim PartTemplate, AssyTemplate As String

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelExtension = swModel.Extension
Set swAssy = swModel

'Path your template here:
AssyTemplate = "C:Template.asmprp" 'Path for Assemblies
PartTemplate = "C:Template.prtprp" 'Path for Parts

'Set template for main assembly
swModelExtension.CustomPropertyBuilderTemplate(False) = AssyTemplate

'Get array of all components inside main assembly
vCmps = swAssy.GetComponents(False)

'Iterate through list of components
For i = 0 To UBound(vCmps)

'skips the model if something goes wrong, i.e. a suppressed model
On Error GoTo Bail:

Set swModel = vCmps(i).GetModelDoc2
Set swModelExtension = swModel.Extension

If swModel.GetType = 1 Then
swModelExtension.CustomPropertyBuilderTemplate(False) = PartTemplate
ElseIf swModel.GetType = 2 Then
swModelExtension.CustomPropertyBuilderTemplate(False) = AssyTemplate
End If

swModel.Save

Bail:

Debug.Print i + 1 & " of " & UBound(vCmps) + 1 & " processed. Currently working on: " & swModel.GetTitle

Next i

End Sub

NJ
NJ 2019-10-22

Hi Josh,

Thank you so much for sharing this code. I've tweaked it a bit thus far:

1) Added a section witch opened a sample Part then jumped back to the Part/Assy we're trying to change the custom property template of, which in turn forces the update.

2) wrote a value to a dummy variable so that the Part/Assy would see it self as "changed" thus permitting saving of the updated Part/Assy.

3) Closed Parts/Assys after they were updated.

4) Closed the Sample Part.

5) Saved the now updated Assembly and Parts.

Right now it works! It's a little slow, partially because has to open every instance of every part and assembly in the feature tree. The coding is beyond me but I'd like to be able to have the macro be smart enough to only open something and update it once - it would save time as we often have nuts/bolts/washers modeled in our file, or multiple instances of a sub assembly.

I've attached my current code below:

' *****************************************************************************************
' * Setting The Custom Property on Parts and Assemblies and Forcing a Refresh - N.Jeffers *
' *****************************************************************************************

Dim swApp As SldWorks.SldWorks

Dim Part As SldWorks.ModelDoc2
Dim PartII As Object
Dim PartIII As SldWorks.ModelDoc2
Dim PartIV As SldWorks.ModelDoc2
Dim PartV As SldWorks.ModelDoc2
Dim DNE As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim loptions As Long
Dim lerrors As Long

Dim swModel As ModelDoc2
Dim swModelExtension As ModelDocExtension
Dim i As Integer
Dim vCmps As Variant
Dim PartTemplate, AssyTemplate As String

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc 'Variable for Custom Property Template
Set swModelExtension = swModel.Extension 'Variable for Custom Property Template
Set swAssy = swModel 'Variable for Custom Property Template

Set Part = swApp.ActiveDoc 'Variable for Refresh

'Path your Custome Property Templates here:
AssyTemplate = "N:\Solidworks Templates\Assembly.asmprp" 'Path for Assemblies Custom Properties Template
PartTemplate = "N:\Solidworks Templates\Parts.prtprp" 'Path for Parts Custom Properties Template

'Set template for main assembly
swModelExtension.CustomPropertyBuilderTemplate(False) = AssyTemplate

'Forces Refresh by Switching Active Window
Set PartII = swApp.OpenDoc6("N:\Solidworks Templates\Custom Properties Refresh.SLDPRT", 1, 1, "", longstatus, longwarnings) 'POINT THIS TO A SMALL PART THAT WILL NEVER MOVE, THIS WILL BE YOUR "TOGGLE PART"
Dim myModelView As Object
Set myModelView = PartII.ActiveView
myModelView.FrameLeft = 0
myModelView.FrameTop = 28
Set myModelView = PartII.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
swApp.ActivateDoc2 "Custom Properties Refresh.SLDPRT", False, longstatus

'Back to Main Assembly
Set PartIII = swApp.ActivateDoc3(Part.GetPathName, True, loptions, lerrors)
Set swCustPropMgr = PartIII.Extension.CustomPropertyManager(Empty)
swCustPropMgr.Add3 "DNE", swCustomInfoNumber, "2", 2 'forces a change that solidworks notices, thus permitting the file to be saved with the new custom propertie template, you could add any dead field to your custom propery template and have it referance that

'Get array of all components inside main assembly
vCmps = swAssy.GetComponents(False)

'Iterate through list of components
For i = 0 To UBound(vCmps)

'skips the model if something goes wrong, i.e. a suppressed model
On Error GoTo Bail:

Set swModel = vCmps(i).GetModelDoc2
Set swModelExtension = swModel.Extension


If swModel.GetType = 1 Then 'Setting Custom Templates for Parts
swModelExtension.CustomPropertyBuilderTemplate(False) = PartTemplate

Set PartIV = swApp.ActivateDoc3(swModel.GetPathName, True, loptions, lerrors) 'Setting this current part active

'Forces Refresh by Switching Active Window
Set PartII = swApp.OpenDoc6("N:\Solidworks Templates\Custom Properties Refresh.SLDPRT", 1, 1, "", longstatus, longwarnings)
Set myModelView = PartII.ActiveView
myModelView.FrameLeft = 0
myModelView.FrameTop = 28
Set myModelView = PartII.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
swApp.ActivateDoc2 "Custom Properties Refresh.SLDPRT", False, longstatus

'Back to Part File
Set PartIV = swApp.ActivateDoc3(swModel.GetPathName, True, loptions, lerrors) 'Setting this current part active, this forces the Custom part update
swCustPropMgr.Add3 "DNE", swCustomInfoNumber, "2", 2 'forces a change that solidworks notices, thus permitting the file to be saved with the new custom propertie template, you could add any dead field to your custom propery template and have it referance that
swApp.CloseDoc PartIV.GetTitle 'Closes open document


ElseIf swModel.GetType = 2 Then
swModelExtension.CustomPropertyBuilderTemplate(False) = AssyTemplate

Set PartV = swApp.ActivateDoc3(swModel.GetPathName, True, loptions, lerrors) 'Setting this current part active

'Forces Refresh by Switching Active Window
Set PartII = swApp.OpenDoc6("N:\Solidworks Templates\Custom Properties Refresh.SLDPRT", 1, 1, "", longstatus, longwarnings)
Set myModelView = PartII.ActiveView
myModelView.FrameLeft = 0
myModelView.FrameTop = 28
Set myModelView = PartII.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
swApp.ActivateDoc2 "Custom Properties Refresh.SLDPRT", False, longstatus

'Back to Assembly File
Set PartV = swApp.ActivateDoc3(swModel.GetPathName, True, loptions, lerrors) 'Setting this current part active, this forces the custom part update
swCustPropMgr.Add3 "DNE", swCustomInfoNumber, "2", 2 'forces a change that solidworks notices, thus permitting the file to be saved with the new custom propertie template, you could add any dead field to your custom propery template and have it referance that
swApp.CloseDoc PartV.GetTitle 'Closes Part


End If

swModel.Save

Bail:

Debug.Print i + 1 & " of " & UBound(vCmps) + 1 & " processed. Currently working on: " & swModel.GetTitle

Next i


Set PartIII = swApp.ActivateDoc3(Part.GetPathName, True, loptions, lerrors) 'This puts you back in the orginal assembly
swApp.CloseDoc PartII.GetTitle 'Closes Part
PartIII.SaveAs PartIII.GetPathName 'Saves changes to Custom Properties

End Sub

I'm sure that there is lots of things that could be improved/corrected.

Thanks again!!!

NP
NP 2021-02-15

Hi Josh,

I know I'm coming in late here, but there's an easier way to refresh the Custom Property Tab - just switch to another tab and back like so. If you've hidden / disabled the default Solidworks Resources tab, just switch to a tab you use (Design Library?).

'Tab IDs: 1 = Design Library, 2 = File Explorer, 3 = Solidworks Resources, 4 = Clipboard, 5 = Custom Properties, 6 = P&ID
'Switch task pane view to Solidworks Resources tab - start of refresh
boolStatus = swApp.ActivateTaskPane(3)
'Switch task pane view to Custom Properties tab - end of refresh
boolStatus = swApp.ActivateTaskPane(5)