Nobody is obliged to write your macro for you.
Anyway, you can easily achieve this without a macro.
For example, Tools -> Assembly Visualization
Add SW-Material as a column.
Sort by that column twice and drag the bottom slider bar above those parts which have a material assigned.
This try to use such commands for your macro:
vComponents = swAssemblyDoc.GetComponents(False)
For Each obj In vComponents
Set swComp = obj
Set swPrt = swComp.GetModelDoc2
material = swPrt.GetMaterialPropertyName2("", Database)
If material = "" Then
boolstat = swComp.Select4(True, swSelData, False)
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swCompModel As ModelDoc2
Dim swMass As SldWorks.MassProperty
Dim vDepend As Variant
vDepend = swApp.GetDocumentDependencies2(swModel.GetPathName, True, True, False)
For i = 0 To (UBound(vDepend) - 1) / 2
If Not swApp.IGetOpenDocumentByName2(vDepend(2 * i + 1)) Is Nothing Then
Set swCompModel = swApp.IGetOpenDocumentByName2(vDepend(2 * i + 1))
Set swMass = swCompModel.Extension.CreateMassProperty
If swMass.UserAssigned = False Then
The reason GetDocumentDependencies is used rather than GetComponents is because GetComponents will get every instance of the same part. GetDocumentDependencies will only get the unique parts. If a part is an assigned mass, every instance of that part it is also an assigned mass. Therefore, you only need the unique parts.
What are you trying to do? I pulled this from a macro I made that found all the assigned masses in an assembly and found their mass and volume so that it could assign them a new material with that density. This allows the mass moments to be calculated in mass properties.
Forgot this after the declarations:
Set swApp = GetObject(, "SldWorks.Application")
Set swModel = swApp.ActiveDoc
Thank you for ideas, it can be done this way as well ; however, in this case I particularly wanted to get COMPONENTS; this allowes to use swComp.SELECT4 & HideComponent2 and user can instantly visualise which parts had unasigned materials.
Well do whatever you please, but depending on how big the assembly is (4000+), GetComponents can be a very time consuming loop. It would be faster to use GetModelDependencies then get the name of each modeldoc and use AdvancedComponentSelection to select and hide every single instance of that part at the same time