This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
This macro demonstrates how to copy the specified custom property from the material database to the model custom property using SOLIDWORKS API and XML parsers.
MSXML2.DOMDocument object is used to read XML of the material database and select the required material node.
Specify the custom property name to copy via PRP_NAME variable
Const PRP_NAME AsString = "MyProperty"
Run the macro. Macro will find the material of active part and read the property value from the material database file
Macro will create/update the generic custom property of the file to the corresponding value of the custom property from material
Const PRP_NAME AsString = "MyProperty"Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swPart As SldWorks.PartDoc
Set swPart = swApp.ActiveDoc
IfNot swPart IsNothingThenDim materialName AsStringDim materialDb AsString
materialDb = GetMaterialDatabase(swPart, materialName)
If materialDb <> ""ThenDim prpVal AsString
prpVal = GetMaterialCustomProperty(materialName, materialDb, PRP_NAME)
SetCustomProperty swPart, PRP_NAME, prpVal
Else
MsgBox "Failed to find the material database"EndIfElse
MsgBox "Please open part"EndIfEndSubFunction GetMaterialDatabase(part As SldWorks.PartDoc, ByRef materialName AsString) AsStringDim materialDbName AsString
materialName = part.GetMaterialPropertyName2("", materialDbName)
Dim vDbs AsVariant
vDbs = swApp.GetMaterialDatabases()
IfNot IsEmpty(vDbs) ThenDim i AsIntegerFor i = 0 To UBound(vDbs)
Dim dbFilePath AsString
dbFilePath = vDbs(i)
Dim dbFileName AsString
dbFileName = Right(dbFilePath, Len(dbFilePath) - InStrRev(dbFilePath, "\"))
If LCase(dbFileName) = LCase(materialDbName & ".sldmat") Then
GetMaterialDatabase = dbFilePath
ExitFunctionEndIfNextEndIf
GetMaterialDatabase = ""EndFunctionFunction GetMaterialCustomProperty(materialName AsString, materialDb AsString, prpName AsString) AsStringDim xmlDoc AsObjectSet xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.Load materialDb
Dim matNode AsObjectSet matNode = xmlDoc.SelectSingleNode("//classification/material[@name='" & materialName & "']/custom/prop[@name='" & prpName & "']")
IfNot matNode IsNothingThen
GetMaterialCustomProperty = matNode.Attributes.GetNamedItem("value").Text
Else
Err.Raise vbError, , "Failed to find the custom property " & prpName & " in material " & materialName & " in database " & materialDb
EndIfEndFunctionSub SetCustomProperty(model As SldWorks.ModelDoc2, prpName AsString, prpVal AsString)
Dim swPrpMgr As SldWorks.CustomPropertyManager
Set swPrpMgr = model.Extension.CustomPropertyManager("")
swPrpMgr.Add3 prpName, swCustomInfoType_e.swCustomInfoText, prpVal, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue
swPrpMgr.Set2 prpName, prpVal
EndSub
Notifications
Join session by SOLIDWORKS and PDM API expert Artem Taturevych at 3DEXPERIENCE World 2026 on Wednesday, Feb 4 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more