Given a part number, how do you determine its row index in a BOM table?

Situation:

     I am currently programming a macro in VBA for use in Solidworks 2010.  I currently have a working macro to copy all BOMs located in all drawing files in our vault to excel, then run a macro to cross-reference the parts that I want to be "hidden" in the solidworks BOM table.  I am currently working on a function to go back to Solidworks and then hide the said rows in the BOMs.  I know very little about Solidworks, but am knowledgable about VBA.

Problem:

     I know how to open a specific drawing, traverse to find and select a specific BOM, but I am having difficulty determining which row in the BOM contains the part number to be hidden.  Given a part number that you know exists in a BOM table, how can you determine row index & then hide said row?

My thoughts:

     I have attempted to use the API help file "Get Components in Each BOM Table Row Example (VBA)" to find the correct row in a BOM, but the line; "swBOMTableAnn.GetComponentsCount2(J, ConfigName, ItemNumber, PartNumber)" is giving me a "Run-time error '438".  I realize the API help example is designed to run off their example file, but I can not determine what the difference is between the example file and my file.

     Is it easier to just delete the BOM table and then import a new "GeneralTable" containing only the items that I want to be there?

SolidworksApi macros
Answers 6
SF
SF 2012-06-05 Best Answer

As soon as I posted, Solidworks reccomended I look at, https://forum.solidworks.com/message/222840#222840.  It had the correct answer.

KR
KR 2012-06-05

Craig,

If I'm understanding your situation correctly, it sounds like all you need to do is find the row with a cell containing a certain piece of text.  That can be done with a lot fewer lines of code that you example you linked to.  While you traverse through each cell you can simply use ITableAnnotation::Text and then the InStr() function to identify if the part number is present.  Then you can use ITableAnnotation::RowHidden to hide the row.

If you want to see a free macro that traverses all text in all BOM table cells then check out the one called "Get cell data in active sheet’s table" under the Drawings section in our Macro Library. (requires a free membership)

As for that run-time error 438, you might also like our free PDF that covers the 15 most common errors you'll get while programming with the SolidWorks API.

Best of luck.

Keith

Video Tutorials for the SolidWorks API

SF
SF 2012-06-06

Keith,

You are correct.  Your code example is exactly what I was looking for and was maybe 1/4 the amount of code I was attempting to use.  It even worked for a seperate issue I was having capturing information in general tables.

Craig Tsukuno

SF
SF 2012-06-06

I have no idea why, but your example and the API help file both claimed the row at the top of the BOM below was row 0.  However, if you look at the label to the left of the table, the row on the top is labeled as row 16.  I don't have a clue why the row starts at 16, but after looking at several of our drawings, they never seem to have a label starting at 0 or 1.

this code found "18" in row 0 but did not hide that row

        For i = 0 To swTable.ColumnCount - 1

            For j = 0 To swTable.RowCount - 1

                If InStr(1, swTable.Text(j, i), "18") Then

                    swTable.RowHidden(j) = True

                End If

            Next j

        Next i

this code hid the row with "18"

     swTable.RowHidden(16) = True

BOM Example Table

comment imagecomment image
KR
KR 2012-06-06

Glad to hear that this was helpful to you.

Concerning your last post, perhaps that could be caused by a custom BOM template that always starts at 16.  Are you using a custom BOM template?  Try using the default one and see if the issue persists.

Keith

Video Tutorials for the SolidWorks API

SF
SF 2012-06-06

It looks like you were on the correct track.

According to Lenny's Solidworks Resources website;

http://www.lennyworks.com/solidworks/default.asp?ID=28

For custom BOM templates

  • Note: if any changes to the assembly are made, you must open the BOM template in the drawing to force an update of the visible columns.

As soon as I reopened the template, the BOM was correct...now to determine how to open the template via a macro.