How to export material property to excel

I am writing a long macro that lists the current part/assembly information (Description, Material, Job Number, Client, Location, etc.)  in order horizontally onto an excel spreadsheet.  When I click the button SolidWorks sends this information to the master part numbering excel spreadsheet.  It grabs the next available row and assigns it a new part number.  I can do all this but I can't get the material custom property to show up in the spreadsheet.  What I want it to say, for example, is "AISI 304" but instead it says "SW-Material@Part1.sldprt".  How do you call that custom property and set it equal to a cell in Excel?

SolidworksApi macros
Answers 3
AH
AH 2015-05-14 Best Answer

Here is what I use. I don't know how your material library is structured but this works for me.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim str As String

Sub main()

Set swApp = _
Application.SldWorks

If swApp.GetDocumentCount() = 0 Then Exit Sub

Set swModel = swApp.ActiveDoc

If Not swModel.GetType() = swDocumentTypes_e.swDocPART Then Exit Sub

str = ""

str = GetMaterialName(swModel)

MsgBox ("The Name Of The Material Is: " + str)

End Sub

Function GetMaterialName(ByVal ModelToCheck As SldWorks.ModelDoc) As String

Dim sMaterialName As String
Dim StartI As Integer
Dim LastI As Integer

GetMaterialName = ""

sMaterialName = ""

If ModelToCheck Is Nothing Then

   Exit Function

End If

sMaterialName = ModelToCheck.MaterialIdName

StartI = InStr(sMaterialName, "|")

LastI = InStrRev(sMaterialName, "|")

If StartI > 0 And LastI > 0 Then

   If StartI = LastI Then
  
      sMaterialName = Right(sMaterialName, Len(sMaterialName) - StartI)
  
      GetMaterialName = sMaterialName
     
   Else
     
      sMaterialName = Mid(sMaterialName, StartI + 1, LastI - StartI - 1)
  
      GetMaterialName = sMaterialName
  
   End If
  
End If

End Function

JL
JL 2015-05-14

Thanks Adam!  I used your function and was able to make it work for my macro. 

AH
AH 2015-05-14

No problem. Glad to help.