Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:10:12+00:00 2026-06-15T12:10:12+00:00

When using the Visual Studio extensibility SDK I have an instance of a ProjectItem

  • 0

When using the Visual Studio extensibility SDK I have an instance of a ProjectItem. I’m trying to get the text from this item so I can do some replacements on it. The way that I’ve seen this done is by using the properties on DTE2.ActiveDocument.Selection. However, the DTE2.ActiveDocument isn’t the document that I need so I can’t use that. When I try to access the ProjectItem.Document object which contains a Selection property the document is always null and I get a null reference exception. I’ve also tried the following which doesn’t work (i.e. the Document is valid, but the Selection property is null):

Document document = null;
if (!projectItem.IsOpen)
    document = projectItem.Open().Document;

I tried the following but it didn’t give me the correct document since the ProjectItem I’m dealing with isn’t the active document. Is there any way to implement something similar to the following code that uses ProjectItem.Document instead?

TextSelection selection = DTE2.ActiveDocument.Selection;
selection.SelectAll();
string text = selection.Text;
selection.Delete();
//Do replacements
selection.Insert(text);

To summarize, how do I get a TextSelection instance from a ProjectItem instance?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:10:13+00:00Added an answer on June 15, 2026 at 12:10 pm

    As usual when dealing with the VS SDK, the answer is a bit obscure. The way that I solved it (right or wrong) is to make the ProjectItem instance the active document and then use the DTE2.ActiveDocument.Selection property to get the text. This is accomplished via the following:

    if (!projectItem.IsOpen)
        projectItem.Open(@"{7651A701-06E5-11D1-8EBD-00A0C90F26EA}").Document.Activate(); //EnvDTE.Constants.vsViewKindCode
    
    TextSelection selection = _vsApp.ActiveDocument.Selection;
    selection.SelectAll();
    string text = selection.Text;
    selection.Delete();
    //Do replacements
    text = ReplaceTemplateValues(text, replacements);
    selection.Insert(text);
    

    Is there a better way?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Visual Studio's Managed Package Framework, how can I inherit from C# so I
Using Visual Studio 2005 As per the title; MSDN and google can't tell me,
(Using Visual Studio 2005 / .NET 2.0) I have a DataSet which is being
Using Visual Studio or Resharper, can I jump to the .aspx page while in
Using Visual Studio 2008, c#, .net 2.0. I have a Windows Forms client application
(Using Visual Studio 2010) I'm trying to create a shared_ptr of an existing class
Using Visual Studio 2010 C++. I have a project which uses MFC and I
Using Visual Studio 2008 and VB.Net: I have a working web app that uses
Using Visual Studio I have a Class Library (C#) where I added a reference
Using Visual Studio's O/R Designer, I have created a number of classes the inherit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.