Microsoft KB Archive/814722

= &quot;Name 'DTE' is not declared&quot; error message while running the &quot;Item Method (General Extensibility)&quot; MSDN sample code =

Article ID: 814722

Article Last Modified on 5/31/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



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:

http://msdn2.microsoft.com/en-us/library/aa301425(VS.71).aspx

 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 .exe

Additional information: Type mismatch.





CAUSE

 * You receive the &quot;Name 'DTE' is not declared&quot; 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:  In Solution Explorer, right-click  , and then click Add References. In the Add Reference dialog box, double-click envdte, and then click OK to add a reference to the EnvDTE namespace to the project.</li>  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; </li>  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(&quot;ProgID&quot;) Visual C# .NET Sample Code EnvDTE.DTE DTE; DTE = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject(&quot;ProgID&quot;); Note Replace   with the ProgID of your EnvDTE object. </li></ol> </li> 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.</li> To resolve the &quot;Type mismatch&quot; error, only pass a parameter that is an Integer or a String to the EnvDTE.Documents.Item method.</li></ul>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> Start Visual Studio .NET. Use Visual Basic .NET or Visual C# .NET to create a new Windows Application project named ItemMethodDemo .</li> In Solution Explorer, right-click Form1, and then click View Code.</li>  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); }  } } </li> On the Build Menu, click Build Solution.</li></ol>

<div class="references_section">