locked
VS 2010 addin: getting selected text from the editor RRS feed

  • Question

  • Coders, I am developing an add in for VS2010 and I am trying to get the selected text in the code editor. so far, i have been searching many webpages and thy all seems to use DTE.ActiveDocument which causes an error in my code. I have written two versions of a method that suppose to return a selected text in the editor but I still get the same error over and over: the error is: An object reference is required for the non-static field, method, or property 'EnvDTE._DTE.ActiveDocument.get' and here are my two versions of the method (only relevant code is showen):

    using EnvDTE;
    
      private string getSelectedText_V1()
      {
        string selectedText = string.Empty;
    
        /*PROBLEM HERE: An object reference is required for the non-static field, method, or property 'EnvDTE._DTE.ActiveDocument.get'*/
        Document doc = DTE.ActiveDocument;
    
        return selectedText;
      }
    
      private string getSelectedText_V2()
      {
        string selectedText = string.Empty;
    
        /*PROBLEM HERE: An object reference is required for the non-static field, method, or property 'EnvDTE._DTE.ActiveDocument.get'*/
        EnvDTE.TextSelection TxtSelection = DTE.ActiveDocument.Selection;
    
    
        return selectedText;
      }
    
    

    Sunday, January 23, 2011 4:01 AM

Answers

  • Hello,

    You said that you were developing an "add in for VS 2010"... ;-) Add-ins do have a Connect class... anyway, if you are creating a package then you can get an instance of EnvDTE.DTE by calling GetService(typeof(EnvDTE.DTE)). There are some issues about when to make that call. See:

    Dr. eX: Why does GetService(typeof(EnvDTE.DTE)) return null?

    http://blogs.msdn.com/b/vsxteam/archive/2008/06/09/dr-ex-why-does-getservice-typeof-envdte-dte-return-null.aspx

    I hope this helps this time.


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Marked as answer by ealshabaan1 Monday, January 24, 2011 12:16 AM
    Sunday, January 23, 2011 7:33 PM

All replies

  • Your Connect class has a class field that is an instance of the EnvDTE.DTE class and that likely is called _applicationObject and it is assigned in the OnConnection method. You have to use _applicationObject.ActiveDocument.

    If you don't have a class field named DTE then the expression DTE.ActiveDocument refers to EnvDTE.DTE, which is a class, and then only static methods are legal.

    FWIW, in a macro DTE refers also to the instance of EnvDTE.DTE that the macro environment provides, so in a macro DTE.ActiveDocument is legal. Likely examples of macros like this one of me (HOWTO: Get the selected text from a Visual Studio .NET macro or add-in ) have confused you.

    Notice also in your V2 version that you have to check that ActiveDocument is not null (because it can) before getting its Selection property.


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Proposed as answer by Carlos J. Quintero Sunday, January 23, 2011 6:50 AM
    • Unproposed as answer by ealshabaan1 Sunday, January 23, 2011 12:17 PM
    Sunday, January 23, 2011 6:50 AM
  • thanks for your reply.

     

    I've search my code and i couldn't locate any class named Connect or any method named OnConnection() . Thus, there is no _applicationobject declared anywhere.

    I have seen some examples online where they use _applicationObject but i cannot figure out where is this object coming from!? 

    Project Info:

    Visual Studio Package.

    VS 2010.

    Sunday, January 23, 2011 7:14 AM
  • Hello,

    You said that you were developing an "add in for VS 2010"... ;-) Add-ins do have a Connect class... anyway, if you are creating a package then you can get an instance of EnvDTE.DTE by calling GetService(typeof(EnvDTE.DTE)). There are some issues about when to make that call. See:

    Dr. eX: Why does GetService(typeof(EnvDTE.DTE)) return null?

    http://blogs.msdn.com/b/vsxteam/archive/2008/06/09/dr-ex-why-does-getservice-typeof-envdte-dte-return-null.aspx

    I hope this helps this time.


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Marked as answer by ealshabaan1 Monday, January 24, 2011 12:16 AM
    Sunday, January 23, 2011 7:33 PM
  • well, I thought VS add in and VS Package are the same. This is becuase I have downloaded the VS SDK and it has added a new section called "Extensibility" in my project types and there is not project type called VS add-in.

    however, i have relized that i need to go to Visual C# > other project types > extensibility and there is VS addin project type.

     

    Thank you.

    Monday, January 24, 2011 12:41 AM