I would use AssemblyDoc::GetComponents to get an array of all the components. Then, use a For Each ... Next loop to run an If function on each item of the array and use it to increment an integer count of how many components' names match "Part1".
Have a look at GetRootComponent3 Method (IConfiguration) in the API help.
It contains 3 code samples for recursively looping through components of assemblies and sub-assemblies.
Then use Matt's suggestion of counting each part name you come across.
I have shared the code snippet to compose the custom BOM.
I solved the problem in two ways...
I found a useful example at http://www.eng-tips.com/viewthread.cfm?qid=198808
this example get an array of all the components and then compare the selected component (through selectbyID2 method in my case) using getpathname, referencedconfiguration and getsuppression methods... Please check it!! works good... only was necessary edit some lines and adjust the code for my specific application.
Thank you very much Matt your tip was really good...
I do not know if is exactly your method, but I tried to use this code:
Dim solidworks As Object
Dim part As Object
Dim boolstatus As Boolean
Set solidworks = Application.SldWorks
Set part = solidworks.ActiveDoc
boolstatus = True
i = 1
Do Until boolstatus = False
boolstatus = part.Extension.SelectByID2("Part1-" & i & "@TestAssembly", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
If boolstatus = True Then i = i + 1
MsgBox (i - 1)
the only one problem with this code is that depends of the id instance order. As you can note, if there is no a constant step in the ID instance component, then the number of elements obtained will be incorrect. What would you add to fix the possible mistake?
Thanks a lot Artem...
Undoubtly I will take a look to GetRootComponent method and then will comment to you... I never have used this method
Regards and thanks for all...