locked
How Do I ... Get all Text of the current active Document? RRS feed

  • Question

  • Hello guys,

    I'm trying to write my very first extension for Visual Studio 2010. What i need is the text from the current Active Document. Sounds simple, but i'm stuck at this for about 2 days. I found a solution, but i think it is not really nice to use CreateEditPoint() in this case:

        private void WindowEvents_WindowActivated(EnvDTE.Window GotFocus, EnvDTE.Window LostFocus)
        {
          if (GotFocus.Type == vsWindowType.vsWindowTypeDocument)
          {
            ActiveTextDocument = GotFocus.DocumentData as TextDocument;
            string text = ActiveTextDocument.CreateEditPoint().GetText(ActiveTextDocument.EndPoint);
          }
        } 

    What I might need is the current IWpfTextView of the active Document - but I don't know how to get this.

    Please help me :)

    Saturday, April 17, 2010 2:10 PM

Answers

  • Hi David,

    I have written a Macro to  get all Text of current active document using CreateEditPoint function, please see following code:

        Dim objTextDoc As TextDocument
        Dim objEditPt As EnvDTE.EditPoint
        Dim docText As String   
    
        ' Get a handle to the new document and create an EditPoint.
        objTextDoc = DTE.ActiveDocument.Object("TextDocument")
        objEditPt = objTextDoc.StartPoint.CreateEditPoint
        
        ' Get all Text of active document
        docText = objEditPt.GetText(objTextDoc.EndPoint)
        MsgBox(docText)

    You can refer to it to  write an Addin or VSpackage.

    Please let me know if you have any comments.

    Best Regards,

    Nancy


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Nancy Shao Friday, April 23, 2010 9:36 AM
    Tuesday, April 20, 2010 8:19 AM

All replies

  • Hello David,

    I don' t think you are on the right track. I'm not sure what you need the text of current active document for,, but i think the right way to do this is to first get the file path/location of the active doc, then using file stream to open this file. Like this:

    private

     

    void WindowEvents_WindowActivated(EnvDTE.Window GotFocus, EnvDTE.Window LostFocus)

    {

     

    string filePath=GotFocus.DTE.ActiveDocument.FullName;

     

    using (FileStream activeDoc = File.Open(filePath,FileMode.OpenOrCreate))

    {

     

    // do anything you want to do ;

    }

     

    }


    Thanks, B.C.
    Tuesday, April 20, 2010 6:43 AM
  • Hi David,

    I have written a Macro to  get all Text of current active document using CreateEditPoint function, please see following code:

        Dim objTextDoc As TextDocument
        Dim objEditPt As EnvDTE.EditPoint
        Dim docText As String   
    
        ' Get a handle to the new document and create an EditPoint.
        objTextDoc = DTE.ActiveDocument.Object("TextDocument")
        objEditPt = objTextDoc.StartPoint.CreateEditPoint
        
        ' Get all Text of active document
        docText = objEditPt.GetText(objTextDoc.EndPoint)
        MsgBox(docText)

    You can refer to it to  write an Addin or VSpackage.

    Please let me know if you have any comments.

    Best Regards,

    Nancy


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Nancy Shao Friday, April 23, 2010 9:36 AM
    Tuesday, April 20, 2010 8:19 AM