Sub CodeElementFromPointExample(ByVal dte As DTE2)
' Before running this example, open a code document from a project
' and place the insertion point anywhere inside the source code.
Try
Dim sel As TextSelection = _
CType(dte.ActiveDocument.Selection, TextSelection)
Dim pnt As TextPoint = CType(sel.ActivePoint, TextPoint)
' Discover every code element containing the insertion point.
Dim fcm As FileCodeModel = _
dte.ActiveDocument.ProjectItem.FileCodeModel
Dim elems As String
Dim elem As CodeElement
Dim scope As vsCMElement
For Each scope In [Enum].GetValues(scope.GetType())
elem = fcm.CodeElementFromPoint(pnt, scope)
If IsNothing(elem) = False Then
elems &= elem.Name & " (" & scope.ToString() & ")" & _
vbCrLf
End If
Next
MsgBox("The following elements contain the insertion point:" _
& vbCrLf & vbCrLf & elems)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub