Logo
BUG: DTE.ActiveWindow.ProjectItem.Document null for files in Solution Items folder.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: February 2014   Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
      Microsoft Visual Studio 2013
Introduction

This article describes a bug in the automation model (EnvDTE) when using files in the Solution Items folder.

More Information

When you call DTE.ActiveWindow.ProjectItem.Document from an add-in, package or macro, you get null for files under the Solution Items folder, but not for files of a project. The steps to reproduce the problem are the following:

  • Create an add-in with the code below.
Language: C#   Copy Code Copy Code (IE only)
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;

namespace MyAddin1
{
   public class Connect : IDTExtensibility2
   {
      private DTE2 _applicationObject;

      public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
      {
         _applicationObject = (DTE2)application;

         switch (connectMode)
         {
            case ext_ConnectMode.ext_cm_Startup:

               // Do nothing; OnStartupComplete will be called
               break;

            case ext_ConnectMode.ext_cm_AfterStartup:

               InitializeAddIn();
               break;
         }
      }

      public void OnStartupComplete(ref Array custom)
      {
         InitializeAddIn();
      }

      private void InitializeAddIn()
      {
         Document doc;

         try
         {
            doc = _applicationObject.ActiveWindow.ProjectItem.Document;

            if (doc == null)
            {
               MessageBox.Show("doc is null");
            }
            else
            {
               MessageBox.Show(doc.Name);
            }
         }
         catch (Exception ex)
         {
            MessageBox.Show(ex.ToString());
         }
      }

      public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
      {
      }

      public void OnAddInsUpdate(ref Array custom)
      {
      }

      public void OnBeginShutdown(ref Array custom)
      {
      }
   }
}
  • Run the add-in but do not load it yet in the second VS instance.
  • Create a C# Class Library project.
  • Ensure that the Class1.cs document window is active and load the add-in through the Add-In Manager. You get a messagebox "Class1.cs".
  • Unload the add-in through the Add-In Manager.
  • Right-click the solution node and click the "Add" > "New Item" context menu and select Text file. The TextFile1.txt file is added under the Solution Items folder.
  • Ensure that the TextFile1.txt document window is active and load the add-in through the Add-In Manager. You get a messagebox "doc is null".

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


Top