I don't think you'll be able to use GetComponentByName. Help specifies:
- Name of the top-level assembly component to get
- I think you'll have to get there another way.
Thanks for your answer Josh... I still think I can get the name of the component even if the component is not at top-level. As a matter of fact, I can retrieve the component's name with GetComponentByName when only one instance of the component is assembled somewhere no matter the level.
OK, what do I know.
Could you attach your macro? Have also tried "SelectByID2" to select the component?
I have not tried the SelectByID2 command. I am not very familiar with it... Here is my code so far:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Set swApp = GetObject(, "SldWorks.Application")
Set Part = swApp.ActiveDoc
Set swComp = Part.GetComponentByName("Medio_cople_SIL-1")
If Worksheets("General").Range("H21").Value = "L" Then
swComp.ReferencedConfiguration = Worksheets("Silenciadores").Range("F41").Value
Th macro is run from the main assembly. It works fine as long as I don't have another Medio_cople_SIL-1 used somewhere else in my complete assembly. That means that SW manages to find this component no matter at what level it is assembled. For exemple, this component is assembled inside a sub-assembly (so it is not at top-level). But when I assemble the same component in another sub-assembly, then I have a problem. Actually, that makes sense because I am calling a component that is used twice in the complete assembly (that I understand!!!!). My question is: what is the proper syntax when using the GetComponentByName command. I already tried with "@" and "/" without any success...
Greg. Can I call you Greg? Buddy. Pal.
It don't work. You're doing it wrong. There is no correct syntax for using GetComponentByName to get non-top-level components. You can't get there from here. Do not pass GO. Do not collect $200.
The fact that it does work for you sometimes when there's only one instance of your subassembly is irrelevant. The Help states that GetComponentByName is only intended for top level components.
If you want to keep doing it wrong, by all means pursue the dream of using GetComponentByName. You shall overcome! (Except you won't.)
Alternately, if you want to do it right, add a few more lines.
Dim aComps As Variant
Dim i As Long
aComps = Part.GetComponents(False)
For i = 0 To UBound(aComps)
Set swComp = aComps(i)
If swComp.Name2 = "[name of top-level component]-[instance number]/[name of child component]-[instance number]" Then Exit For
But once again... What do I know. I've only been writing SolidWorks code for 15 years.
Hi Josh... Thanks for your reply... After doing some research, that's the type of code I ended up writing last night.