"Name 'DTE' is not declared" error message while running the "Item Method (General Extensibility)" MSDN sample code (814722)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

This article describes problems that you may experience when you follow the documentation for the Item Method (General Extensibility) that appears in the following Microsoft Developer Network (MSDN) article:
  • When you paste the sample code from the Example section of the article to a Visual Basic .NET application, and then you try to compile the file, you receive the following error message:
    Name 'DTE' is not declared.
  • When you try to assign the return value of the EnvDTE.Documents.Item method to an AddIn type variable, you receive the following error message:
    An unhandled exception of type 'System.InvalidCastException' occurred in ApplicationName.exe
    Additional information: Specified cast is not valid.
  • When you pass an Object parameter to the EnvDTE.Documents.Item method, you receive the following error message:
    An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in ApplicationName.exe
    Additional information: Type mismatch.

CAUSE

  • You receive the "Name 'DTE' is not declared" error message because the variable named DTE is not declared in the sample code that is provided in the MSDN article.
  • You receive the InvalidCastException error message because the MSDN article states that the return value for the EnvDTE.Documents.Item method is an AddIn type. However, the EnvDTE.Documents.Item method returns a Document type object. Therefore, when you try to assign a Document type object to an AddIn type variable, you receive the error message.
  • You receive the Type mismatch error message when you try to pass a parameter that is not an Integer or a String to the EnvDTE.Documents.Item method. The EnvDTE.Documents.Item method is used to gain access to the elements of the EnvDTE.Documents collection. The EnvDTE.Documents.Item method can accept only two types of parameters:
    • Integer - for the index value of the documents in the EnvDTE.Documents collection
    • String - for the key of the documents in the EnvDTE.Documents collection

RESOLUTION

  • To use the variable named DTE, follow these steps:
    1. In Solution Explorer, right-click Your Project Name, and then click Add References.
    2. In the Add Reference dialog box, double-click envdte, and then click OK to add a reference to the EnvDTE namespace to the project.
    3. Add the following statement to the top of your Visual Studio .NET file:

      Visual Basic .NET Sample Code
      Imports EnvDTE
      Visual C# .NET Sample Code
      using EnvDTE;
    4. Add the following statements to the beginning of the ItemExample1 method:
      Visual Basic .NET Sample Code
      Dim DTE As EnvDTE.DTE
      DTE = System.Runtime.InteropServices.Marshal.GetActiveObject("ProgID")
      Visual C# .NET Sample Code
      EnvDTE.DTE DTE;
      DTE = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("ProgID");
      Note Replace ProgID with the ProgID of your EnvDTE object.
  • To resolve the InvalidCastException error, instead of assigning the return value of the EnvDTE.Documents.Item method to an AddIn type variable, assign the return value to a Document type variable.
  • To resolve the "Type mismatch" error, only pass a parameter that is an Integer or a String to the EnvDTE.Documents.Item method.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Visual Studio .NET. Use Visual Basic .NET or Visual C# .NET to create a new Windows Application project named ItemMethodDemo.
  2. In Solution Explorer, right-click Form1, and then click View Code.
  3. Add the following sample code for a method (from the MSDN article) to Form1:

    Visual Basic .NET Sample Code
    Sub ItemExample1()
       ' Closes all saved documents.
       Dim iDoc As Integer
       For iDoc = 1 To DTE.Documents.Count
          If DTE.Documents.Item(iDoc).Saved Then
             DTE.Documents.Item(iDoc).Close()
          End If
       Next iDoc
    End Sub
    
    Visual C# .NET Sample Code
    void ItemExample1()
    {
       //Closes all saved documents.
       int iDoc;
       for(iDoc=1;iDoc<DTE.Documents.Count;++iDoc)
       {
          if (DTE.Documents.Item(iDoc).Saved)
          {
             DTE.Documents.Item(iDoc).Close(EnvDTE.vsSaveChanges.vsSaveChangesYes);
          }
       }
    }
  4. On the Build Menu, click Build Solution.

Modification Type:MinorLast Reviewed:2/2/2006
Keywords:kbvs2005doesnotapply kbvs2005swept kberrmsg kbCollections kbprb kbhelp kbenv kbAutomation kbDocs kbCOMInterop kbMarshal kbdocfix kbdocerr KB814722 kbAudDeveloper