Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 814722 - Last Review: May 31, 2007 - Revision: 1.5
"Name 'DTE' is not declared" error message while running the "Item Method (General Extensibility)" MSDN sample code
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.
- 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
- To use the variable named DTE, follow these steps:
- In Solution Explorer, right-click
Your Project Name, 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.
- Add the following statement to the top of your Visual
Studio .NET file:
Visual Basic .NET Sample CodeVisual C# .NET Sample Code - Add the following statements to the beginning of the ItemExample1 method:
Visual Basic .NET Sample CodeDim DTE As EnvDTE.DTE
DTE = System.Runtime.InteropServices.Marshal.GetActiveObject("ProgID") Visual C# .NET Sample CodeEnvDTE.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.
This
behavior is by design.
Steps to Reproduce the Behavior
- Start Visual Studio .NET. Use Visual Basic .NET or Visual C# .NET to create a new Windows Application project
named ItemMethodDemo.
- In Solution Explorer, right-click
Form1, and then click View Code.
- Add the following sample code for a method (from the MSDN article) to
Form1:
Visual Basic .NET Sample CodeSub 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 Codevoid 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);
}
}
} - On the Build Menu, click Build
Solution.
For more information, visit the following Microsoft Web sites:
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
| kbvs2005doesnotapply kbvs2005swept kberrmsg kbcollections kbprb kbhelp kbenv kbautomation kbdocs kbcominterop kbmarshal kbdocfix kbdocerr KB814722 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate