Problem getting Material Properties on a component in an Assembly

I’m having some trouble with some code that I’m writing to assign colors to components in an assembly.  Sometimes swMaterialProps returns nothing from the swComponent.MaterialPropertyValues call. But this is happening completely randomly. Sometimes the call returns a value for a part and sometimes it doesn't for the same part.

Case swDocumentTypes_e.swDocASSEMBLY

                Dim clsSelectionManager As swSelectionManager = New swSelectionManager

                Dim swAssemblyDoc As AssemblyDoc = swModel

                If clsSelectionManager.IsSelection(swApp) Then

                    'Change color on selected components

                Else

                    'Change color on all components

                    Try

                        Dim objComponents() As Object = swAssemblyDoc.GetComponents(True)

                        For Each objComponent As Object In objComponents

                            Dim swComponent As Component2 = objComponent

                            Dim swMaterialProps As Object = swComponent.MaterialPropertyValues

                            Dim j As Double = 0

                            If Not swMaterialProps Is Nothing Then

                                Debug.Print(swComponent.Name2 & " before color change")

                                Debug.Print("  " & swMaterialProps(0))

                                Debug.Print("  " & swMaterialProps(1))

                                Debug.Print("  " & swMaterialProps(2))

                                Do Until j > 0.3 And j < 2.7

                                    j = 0

                                    For i = 0 To 2

                                        swMaterialProps(i) = clsRandom.NextDouble

                                        j = swMaterialProps(i) + j

                                    Next

                                Loop

                                Debug.Print(swComponent.Name2 & " after color change")

                                Debug.Print("  " & swMaterialProps(0))

                                Debug.Print("  " & swMaterialProps(1))

                                Debug.Print("  " & swMaterialProps(2))

                                swComponent.MaterialPropertyValues = swMaterialProps

                            Else

                                MsgBox("Unable to change color for " & swComponent.Name2)

                            End If

                            swComponent = Nothing

                            swMaterialProps = Nothing

                        Next

                    Catch ex As Exception

                        MsgBox("subRandomColor(All parts in Assembly): " & ex.Message, MsgBoxStyle.Critical, "Error")

                        Dim errorlog As New ErrorLog

                        errorlog.SendErrorToLogFile(swModel.GetPathName, clsCC.strUserInitials(), Format(Now, "short date"), ex.Message, ex.StackTrace)

                    End Try

                End If

        End Select

SolidworksApi macros