Because ActiveDocument.Text() Would Be Too Easy...
October 2, 2008 5:31 PM   Subscribe

I'm writing a code analysis tool in VB.NET for Visual Studio 2005. Help me extract the text of the active window.

My plan: Create an add-in that will put an "Analyze" item in the Tools menu. The developer opens her code file, clicks the menu item, and all of the text in the currently-active window is passed to the analysis engine I am writing.

What I can't seem to crack is how to get all of the text in the active window. Googling has provided some insight as to how to get TextPoint and EditPoint objects, but nothing that takes me all the way to just having a string (or some reasonable object) that contains a bunch of text for me to process.

Is this even possible? If not, I'd be happy for suggestions as to how I can divine the full path to the disk file that contains the text in the active window. Many thanks.
posted by DWRoelands to Computers & Internet (3 answers total) 1 user marked this as a favorite
 
Check out this guy's series on writing a pokerbot. Probably more than you need to know about reading text from windows, and a bunch of other cool stuff too.
posted by sanko at 5:35 PM on October 2, 2008 [1 favorite]


Best answer: Rather than writing a standalone analysis program, have you considered writing a Visual Studio plugin that expose explicit APIs that do what you want (among a whole host of other things). There are also frameworks for making plugin writing easier than what Microsoft provides.
posted by mmascolino at 6:43 PM on October 2, 2008 [1 favorite]


Best answer: More specifically, you'll want to look at the Automation and Extensibility APIs for VS. The EnvDTE and EnvDTE80 namespaces will be particularly important.

Sub PathExample()
   Dim doc As Document
   Dim docPath As String

   doc = DTE.ActiveDocument
   docPath = doc.Path
End Sub


would get you the path to the current document.

This is the easiest (ha!) way I can come up with to get the text of a document.

Sub TextExample()
   Dim doc as TextDocument
   Dim ep As EditPoint
   Dim docText as String

   doc = DTE.ActiveDocument.Object("TextDocument")
   ep = doc.StartPoint.CreateEditPoint
   docText = ep.GetText(doc.EndPoint.CreateEditPoint)

End Sub


Sorry for any VB.Net errors. I normally code in C#, so I'm not as familiar with VB syntax.
posted by jedicus at 10:22 PM on October 2, 2008


« Older Great Depression movies   |   What's the best way to book a rail ticket from... Newer »
This thread is closed to new comments.