Create a new macro in SolidWorks. Cut and paste this code into it. All will be right with the world.
Option Explicit
Dim swApp As SldWorks.SldWorks Dim Part As SldWorks.ModelDoc2 Dim SelMgr As SldWorks.SelectionMgr Dim component As SldWorks.Component2 Dim parent As SldWorks.Component2 Dim assembly As SldWorks.AssemblyDoc Dim entity As SldWorks.entity Dim seldata As SldWorks.SelectData Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim componentName As String Dim assemblyName As String Dim selType As Long
Sub main()
On Error GoTo errorhandler
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc Set SelMgr = Part.SelectionManager Set seldata = SelMgr.CreateSelectData
'Check to see that we have an assembly If Part.GetType 2 Then MsgBox "This macro only works on assemblies.": End
'Figure out what the user selected selType = SelMgr.GetSelectedObjectType3(1, 0)
'Make sure user has selected valid entity from graphics view If selType = swSelEDGES Or selType = swSelFACES Or selType = swSelVERTICES Then Set entity = SelMgr.GetSelectedObject5(1) Set component = entity.GetComponent ElseIf selType = swSelCOMPONENTS Then Set component = SelMgr.GetSelectedObject5(1) If component Is Nothing Then MsgBox "You probably have the topmost assembly object selected.": End Else MsgBox "Select a component from the FM tree or a component's face,edge, or vertex from graphics view." End End If
'Select the component's parent Set parent = component.GetParent If parent Is Nothing Then Part.Extension.SelectByID2 Part.GetTitle, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0 Else boolstatus = parent.Select3(False, seldata) End If
errorhandler: MsgBox "Something went horribly wrong!"
End Sub