I'm currently working on a project in SolidWorks 2024 and attempting to access and copy material sub-properties, such as Description and Source, from the material properties tab to the File|Properties tab using VBA. Despite various attempts, I've encountered persistent difficulties.
Here's a brief overview of the steps I've followed:
Environment Setup: Ensured all necessary references are checked (SolidWorks Type Library, SolidWorks Constant Type Library) in the VBA editor.
Material Assignment: Confirmed the material is assigned to the part, and the properties like Description and Source are correctly defined within the material.
Attempted Code:
vba
Copy
Sub CopyMaterialPropertiesFromMaterialDatabase() Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim swCustomPropMgr As SldWorks.CustomPropertyManager Dim materialName As String Dim description As String Dim source As String Dim resolved As Boolean ' Get the SolidWorks application Set swApp = Application.SldWorks ' Get the active model Set swModel = swApp.ActiveDoc ' Check if the model is a part or assembly If swModel.GetType <> swDocPART And swModel.GetType <> swDocASSEMBLY Then MsgBox "Please open a part or assembly document." Exit Sub End If ' Get the Custom Property Manager for the active configuration Set swCustomPropMgr = swModel.Extension.CustomPropertyManager(swModel.ConfigurationManager.ActiveConfiguration.Name) ' Get the material name assigned to the part materialName = swModel.GetMaterialPropertyName2("") If materialName = "" Then MsgBox "No material assigned to the part." Exit Sub End If ' Get the Material Custom Property Manager Dim swMaterialCustPropMgr As Object Set swMaterialCustPropMgr = swModel.MaterialUserPropertyValues2 ' Get the material properties resolved = swMaterialCustPropMgr.Get4("Description", False, "", description) If Not resolved Or description = "" Then MsgBox "Material description not found." Exit Sub End If resolved = swMaterialCustPropMgr.Get4("Source", False, "", source) If Not resolved Or source = "" Then MsgBox "Material source not found." Exit Sub End If ' Add properties to custom properties swCustomPropMgr.Add3 "Material Description", 30, description, 0 swCustomPropMgr.Add3 "Material Source", 30, source, 0 MsgBox "Material properties copied successfully." ' Clean up Set swCustomPropMgr = Nothing Set swMaterialCustPropMgr = Nothing Set swModel = Nothing Set swApp = Nothing End Sub
Despite various modifications, I keep encountering runtime errors, such as "Argument not optional" and difficulties in retrieving the material sub-properties.
Could you please provide guidance or correct the code to successfully access and copy the material sub-properties (Description, Source) to the custom properties tab?
Thank you very much for your assistance.
Nelson
from here...
to here...