Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In VisualStudio DTE, how to get the contents of the ActiveDocument?

I'm scripting inside VisualStudio and am trying to get the contents of the currently ActiveDocument.

This is my current solution:

var visualStudio = new API_VisualStudio_2010();

var vsDTE = visualStudio.VsAddIn.VS_Dte;

var document = (Document)vsDTE.ActiveDocument;
var textDocument = (TextDocument)document.Object("TextDocument");

var editPoint = textDocument.StartPoint.CreateEditPoint();
var text = editPoint.GetText(textDocument.EndPoint.CreateEditPoint());

panel.clear().add_SourceCodeViewer()
     .set_Text(text,  document.FullName.extension());

Is this the best way?

I got the solution from: Because ActiveDocument.Text() Would Be Too Easy...

like image 253
Dinis Cruz Avatar asked May 15 '12 17:05

Dinis Cruz


1 Answers

This is working for me

protected DTE2 dte;
dte2 = (EnvDTE80.DTE2)GetService(typeof(EnvDTE.DTE));

public string GetCurrentTextFile(){

  TextDocument doc = (TextDocument)(dte.ActiveDocument.Object("TextDocument"));
  var p = doc.StartPoint.CreateEditPoint();
  string s = p.GetText(doc.EndPoint);

  return s;            
}
like image 80
Frederic Torres Avatar answered Oct 06 '22 11:10

Frederic Torres