What code should I use to extract the name of the BOM from the Feature Tree in a drawing?

I'm trying to get the code to be able to extract the name of the BOM ("Bill of Materials1" in the picture) to be able to use that name, even if it changes, in a macro.

Thanks

SolidworksApi macros
Answers 5
AH
AH 2015-04-30 Best Answer

Here is one example of how you could do it. Although, you might have to do some extra checking in the case if a drawing had more than one BOM on a sheet.

Dim swApp                 As SldWorks.SldWorks

Dim swModel              As SldWorks.ModelDoc2

Dim swFeat                As SldWorks.Feature

Dim sBOMName        As String

Set swApp = Application.SldWorks

If swApp.GetDocumentCount() = 0 Then Exit Sub

Set swModel = swApp.ActiveDoc

If swModel Is Nothing Or swModel.GetType <> swDocumentTypes_e.swDocDRAWING Then

       MsgBox ("You Must Run This Macro Inside A Drawing")

       Exit Sub

     

End If

sBOMName = ""

Set swFeat = swModel.FirstFeature

Do While Not swFeat Is Nothing

  

       If (swFeat.GetTypeName = "BomFeat") Then

     

          sBOMName = swFeat.Name

          Exit Do

        

       Else

     

          Set swFeat = swFeat.GetNextFeature

        

       End If

     

Loop

If sBOMName = "" Then

   Debug.Print ("There Was No BOM Found On This Sheet")

Else

   Debug.Print ("The Name Of The BOM Is: " + sBOMName)

End If

SF
SF 2015-04-30

Adam,

Thanks for the quick response.  I tried to use the code you gave me but I keep running into the same issue as with some other ways I tried.  I keep getting "Compile Error: Can't Find Project or Library" for swFeat.  What do you think might be causing this error?

Thanks

AH
AH 2015-04-30

Sounds like a reference error for the macro. Try this, open your macro, copy all the text and paste into notepad. Then close your macro, start a recording of a new macro, stop recording, save it with the same name in a different location. Then go to edit that macro and paste the code from your previous macro into it. See if that solves your references issue. Also, what version of Solidworks are you using?

SF
SF 2015-04-30

Adam,

BINGO... You are the man!! I've got it to work now. Thanks for your help.  oh and I am using 2015!!

AH
AH 2015-04-30

No problem. Glad to help.