Get Component for Selected Entity Example (VBA)
This example shows how to get the component to which the selected entity
belongs.
'--------------------------------------------
'
' Preconditions: Model document is open and an entity
is selected.
'
' Postconditions: None
'
'--------------------------------------------
Option Explicit
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swEnt As
SldWorks.entity
Dim
swComp As
SldWorks.Component2
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swEnt = swSelMgr.GetSelectedObject5(1)
Set
swComp = swEnt.GetComponent
Debug.Print
"File = " & swModel.GetPathName
If
Not swComp Is Nothing Then
Debug.Print
" "
& swComp.Name2 & "
--> " & swComp.GetPathName
Else
Debug.Print
" Could
not get component"
End
If
End Sub
'--------------------------------------------