Symptom This article describes the following Microsoft Developer Network (MSDN) article in reading the following Microsoft Developer Network (MSDN) article
Item Method (General EXTENSIBILITY) Documentation may encounter problems:
http://msdn.microsoft.com/library/en-us/vsintro7/html/vxmthitemmethod.asp
If you paste the sample code from this example part to the Visual Basic .NET application and try to compile the file, the following error message appears:
Name 'DTE' is Not Declared.
The following error message appears when trying to assign an envdte.documents.item method to an ADDIN type variable:
An Unhandled Exception of Type 'System.invalidcastException' occurred in ApplicationName.exe
Additional Information: Specified Cast Is Not Valid.
When passing an Object parameter to an envdte.documents.Item method, the following error message appears:
An unhandled exception of type 'system.Runtime.Interopservices.Comexception' occurred in
ApplicationName.exe
Additional Information: Type mismatch.
the reason
The reason for the "Name 'DTE' IS Not Declared" error message is that variables are not declared in the sample code provided in the MSDN article. The reason for the InvalidCastException error message is that the MSDN article mentioned that the return value of the envdte.Documents.Item method is AddIn type. However, Envdte.Documents.Item method returns a Document type object. Therefore, this error message will appear when trying to assign the Document type object to the addin type variable. When the parameter passed to the envdte.documents.Item method is not Integer or String, a Type Mismatch error message will appear. Envdte.Documents.Item method is used to get access to Envdte.Documents collection elements. ENVDTE.Documents.item method only accepts two types of parameters:
Integer - ENVDTE.Documents' index value of the document in the collection. String - the key of the document in the envdte.documents collection.
solution
To use a variable named DTE, follow these steps:
In the Solution Explorer, right-click Your Project Name, then click Add Reference. In the Add Reference dialog box, double-click Envdte, and then click OK to add an Envdte namespace reference to the project. Add the following statement to the top of the Visual Studio .NET file: Visual Basic .NET sample code IMPORTS ENVDTEVISUAL C # .NET Sample Code Using Envdte; Add the following statement to the beginning of the ItemExample1 method: Visual Basic .NET sample code 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 the Envdte object. To correct InvalidCastException errors, not assign the return value of the envdte.documents.Item method to the AddIn type variable, but assign the return value to the Document type variable. To correct the "Type Mismatch" error, only the parameters of Integer or String are passed to the Envdte.Documents.Item method.
This phenomenon is designed to make.
More information
Steps to reproduce problems
Start Visual Studio .NET. Use Visual Basic .NET or Visual C # .NET to create a new "Windows Application" project named ItemMethodDemo. In the Solution Explorer, right-click "FORM1" and click View Code. Add the following sample code of 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 { IF (DTE.Documents.Item (IDOC) .saved) { DTE.Documents.Item (IDoc) .close (Envdte.Vssavechanges.vssAVechangesyes); } } } On the Generate menu, click Generate Solutions. Refer to more information, please visit the Microsoft Web site below: http://msdn.microsoft.com/library/en-us/vsintro7/html/vxmthitemmethod.asp http://msdn.microsoft.com/library/en-us/vsintro7/html/vxlrfdteObject.asp Http://msdn.microsoft.com/library/en-us/vsintro7/html/vxlrfdocumentscollection.asp The information in this article applies to: 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)