"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)
SYMPTOMSThis 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:
- 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 CodeImports EnvDTE Visual C# .NET Sample Codeusing EnvDTE; - 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.
STATUS This
behavior is by design.REFERENCESFor more information, visit the following Microsoft Web sites:
Modification Type: | Minor | Last Reviewed: | 2/2/2006 |
---|
Keywords: | kbvs2005doesnotapply kbvs2005swept kberrmsg kbCollections kbprb kbhelp kbenv kbAutomation kbDocs kbCOMInterop kbMarshal kbdocfix kbdocerr KB814722 kbAudDeveloper |
---|
|