Try this macro:
Const COMP_REF As String = "R12"
Dim swApp As SldWorks.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Set swApp = Application.SldWorks
Set swAssy = swApp.ActiveDoc
Dim vComps As Variant
vComps = swAssy.GetComponents(False)
Dim i As Integer
For i = 0 To UBound(vComps)
Dim swComp As SldWorks.Component2
Set swComp = vComps(i)
If swComp.ComponentReference = COMP_REF Then
swComp.Select4 True, Nothing, False
Regards, Artem Taturevych | Snr. Developer | IC3D ANZ
IC3DSteel – New Steel Solution for SolidWorks
translationXpert – SolidWorks files language translator
Thanks, but that's what I was trying to avoid. I could have hundreds of components, so I would need to do that hundreds of times. That's why I'm looking for a way to select by the reference.
boolstatus = Part.Extension.SelectByID2(...... here's where I tried using ComponentReference
I have experience writing macros that process huge assemblies. If your assembly is a few hundred components, and you only have to run this macro occasionally, you should be fine.
If you have to run this macro many times within the SAME assembly, and during that time you are not adding new components that would need to be searched for, then the macro could be modified to generate a list of all component names and their corresponding reference numbers and store that data as a string in an invisible attribute (or even in a text file on your hard drive if you wanted). When the macro is run, it will first see if this attribute exists. If yes, then it will search the attribute string for the component reference you specify and select the associated component using IModelDocExtension::SelectByID2. Unless you had thousands of pairs in the string, the search would be nearly instantaneous to you.
It looks like I'll have to do it the long way. That's not a huge problem, I just get frustrated when I know there is information in there and I can't access it! I was hoping someone knew an undocumented object.
I ended up storing the values I want to look for in an array, since that's a smaller set than the components in the assembly.