Use EditPoint Object to Get All Code From Code Window | | How do I retrieve all of the code from a Visual Studio .NET Code Window, without disturbing any text selection that may exist in the window? The answer is, "use the EditPoint Object." Using only the TextSelection object of the ActiveDocument object will disturb the current selection of text if any exists.
The Text Editor in the Visual Studio .NET IDE has an edit buffer that shadows the visible text window. The visible text window is manipulated by using the TextSeletion object. The edit buffer, that operates behind the scene, is manipulated by the use of the EditPoint object.
The following line of code is used to call the method that returns the entire contents of the active code window.
Dim s As String = GetAllCodeFromActiveWindow()
|
Figure 1 shows the code for retrieving the code from the active code window in the IDE. This code will work in a macro as well as an add-in. To use the code in a macro, simply replace oVB (applicationObject) DTE. Also, you will have to create a macro Sub to call the Function as you cannot execute a Function macro directly in the Macro IDE.
Figure 1 - GetAllCodeFromActiveWindow Method (Add-in Version).
Public Function GetAllCodeFromActiveWindow() As String
' Returns the code of the whole window, without destroying
' the selected text in the TextSelection object.
Try
Dim ts As TextSelection = oVB.ActiveDocument.Selection
Dim ep As EditPoint = ts.ActivePoint.CreateEditPoint
ep.EndOfDocument()
Dim s As String = ep.GetLines(1, ep.Line)
Return s
Catch ex As System.Exception
' StructuredErrorHandler(ex)
End Try
End Function
| |