Microsoft KB Archive/317345

= How to create and load add-ins in the Visual Studio .NET IDE by using Visual Basic .NET =

Article ID: 317345

Article Last Modified on 3/4/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q317345





For a Microsoft Visual Basic 6.0 version of this article, see 189468.

IN THIS TASK
SUMMARY Requirements Create and Load Add-ins in Visual Studio .NET
 * Create the Visual Basic Project
 * Complete the Steps in the Extensibility Wizard
 * Update the Connect.vb File and Add Custom Actions
 * Build and Install the Add-in Project

Add an Icon to the Toolbar Complete Code Listing (Connect.vb) Verify That It Works Uninstall the Add-in Troubleshooting REFERENCES



SUMMARY
This article demonstrates how to create a simple, compiled add-in that inserts the current date and time at the insertion point. In this article, you create and install an add-in project, load the add-in, and integrate the add-in into the Visual Studio .NET interface.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET
 * Microsoft Windows Server 2003, Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows XP Professional, Microsoft Windows XP Server with Microsoft .NET Framework

back to the top

Create the Visual Basic Project

 * 1) On the Start menu, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.
 * 2) On the File menu, point to New, and then click New Project.
 * 3) Under Project Types, click to expand Other Projects, and then click Extensibility Projects.
 * 4) Under Templates, click Visual Studio .NET Add-in.
 * 5) In the Name text box, type InsertDateTime.
 * 6) In the Location text box, type C:\, and then click OK. This starts the Extensibility Wizard.

back to the top

Complete the Steps in the Extensibility Wizard
 On the first page of the Extensibility Wizard, click Next. On the Select a Programming Language page, click Create an Add-in using Visual Basic, and then click Next. On the Select an Application Host page, clear the Microsoft VSMacros IDEcheck box, and then click Next. On the Enter a Name and Description page, type Insert Date and Time in the Name text box. In the Description text box, type Inserts the current date and time, and then click Next. On the Choose Add-in Options page, select the following check box to create an item on the Tools menu:

'''Yes, create a Tools menu item. By default this will cause the add-in to load when the button is clicked unless the add-in is set to load on startup of the host application.'''

 On the Choose Add-in Options page, select the following check box so that the add-in loads when the host application loads:

I would like my Add-in to load when the host application starts.

Click Next.</li> On the Choosing 'Help About' Information page, click Next.</li> On the Summary page, click Finish. This creates a solution with both an add-in project and an add-in setup project.</li></ol>

back to the top

Update the Connect.vb File and Add Custom Actions
<ol> In Solution Explorer, double-click Connect.vb, and then scroll through the file. Notice that Visual Studio .NET inserts the necessary code templates by default. For this article, you only need to do the following: <ol style="list-style-type: lower-alpha;"> Add a subroutine that writes the date and the time.</li> Modify the Exec subroutine.</li></ol> </li>  Below the Public Sub Exec subroutine, add the following code: Public Function InsertDateTime As Boolean If Not IsNothing(applicationObject.ActiveDocument) Then CType(applicationObject.ActiveDocument.Selection, TextSelection).Text = DateTime.Now.ToString End If       Return True End Function </li>  In Public Sub Exec, change the following code handled = True to: handled = InsertDateTime </li> In Solution Explorer, right-click InsertDateTimeSetup, point to View, and then click Custom Actions.</li> Right-click Custom Actions, and then click Add Custom Action.</li> Click Application Folder, and then click OK.</li> Under InsertDateTime (Active), click Primary output, and then click OK. Notice that primary output appears in the Install, the Commit, the Rollback, and the Uninstall nodes within the Custom Actions node.</li></ol>

back to the top

Build and Install the Add-in Project
<ol> Because setup projects are not included in the build configuration by default, you must use one of the following methods to build the solution: <ul> Right-click InsertDateTime, and then click Build. Similarly, right-click InsertDateTimeSetup, and then click Build.</li> To build the entire solution at once, click Configuration Manager on the Build menu. Select the Build check box for InsertDateTimeSetup.</li></ul> </li> Press the CTRL+SHIFT+B key combination to build the entire solution. A complete installation package is now available for InsertDateTime.</li> To install the add-in that you just built, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Close all instances of Visual Studio .NET, and then save any changes if you are prompted.</li> <li>Open Windows Explorer, and then browse to the following folder:

C:\InsertDateTime\InsertDateTimeSetup\Debug

</li> <li>Right-click InsertDateTimeSetup.msi, and then click Install.</li> <li>In the InsertDateTimeSetup dialog box, click Next three times. Notice that a progress bar appears while the service installs.</li> <li>After the add-in is installed, click Close.</li> <li>Restart Visual Studio .NET.

NOTE: After you restart Visual Studio .NET, the add-in is always loaded until you uninstall it.</li></ol> </li></ol>

back to the top

Add an Icon to the Toolbar
It is helpful, as well as instructive, to integrate your add-in more fully into the Visual Studio integrated development environment (IDE). To do this, add an icon to the toolbar, and then associate the add-in with a keyboard shortcut.
 * 1) On the Tools menu, click Customize.
 * 2) On the Commands tab, in the Categories list, click Addins.
 * 3) Drag InsertDateTime to an active toolbar, and then click Keyboard.
 * 4) In the Show commands containing text box, type insertdate . Notice that your add-in, named InsertDateTime.Connect.InsertDateTime, appears in the list below.
 * 5) In the Press shortcut key(s) text box, press the CTRL+SHIFT+RIGHT ARROW key combination. Notice that the Shortcut currently used by text box notifies you that the Edit.SizeControlRight command already uses this keyboard shortcut.
 * 6) Press the BACKSPACE key to delete the key combination. Press the CTRL+SHIFT+BACKSPACE key combination. Because no other command uses this keyboard shortcut, click Assign.
 * 7) When you are prompted, click Yes, and then type any name for the new keyboard scheme. This new keyboard scheme is a copy of the default scheme with the new add-in mapping.
 * 8) Click Assign, and then click OK.
 * 9) In the Customize dialog box, click Close.
 * 10) In the text file, click InsertDateTime on the toolbar. Notice that the date and the time are inserted. Alternately, you can press CTRL+SHIFT+BACKSPACE to achieve the same results.

back to the top

Complete Code Listing (Connect.vb)
Imports Microsoft.Office.Core imports Extensibility imports System.Runtime.InteropServices Imports EnvDTE

' When run, the Add-in wizard prepared the registry for the Add-in. ' At a later time, if the Add-in becomes unavailable for reasons such as: '  1) You moved this project to a computer other than which it was originally created on. '   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in. '  3) Registry corruption. ' you will need to re-register the Add-in by building the InsertDateTimeSetup project ' by right clicking the project in the Solution Explorer, then choosing install.
 * 1) Region &quot; Read me for Add-in installation and setup information. &quot;
 * 1) End Region

<GuidAttribute(&quot;0045A524-BA5F-43F4-890B-C729D9A12FDB&quot;), ProgIdAttribute(&quot;InsertDateTime.Connect&quot;)> _ Public Class Connect Implements Extensibility.IDTExtensibility2 Implements IDTCommandTarget Dim applicationObject As EnvDTE.DTE Dim addInInstance as EnvDTE.AddIn Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown End Sub Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate End Sub Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete End Sub Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection End Sub Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection applicationObject = CType(application, EnvDTE.DTE) addInInstance = CType(addInInst, EnvDTE.AddIn) If connectMode = Extensibility.ext_ConnectMode.ext_cm_UISetup Then Dim objAddIn As AddIn = CType(addInInst, AddIn) Dim CommandObj As Command

' When run, the Add-in wizard prepared the registry for the Add-in. ' At a later time, the Add-in or its commands may become unavailable for reasons such as: '  1) You moved this project to a computer other than which it was originally created on.            '   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in. '  3) You add new commands or modify commands already defined.            ' You will need to re-register the Add-in by building the InsertDateTimeSetup project,            ' right-clicking the project in the Solution Explorer, and then choosing install.            ' Alternatively, you could execute the ReCreateCommands.reg file the Add-in Wizard generated in            ' the project directory, or run 'devenv /setup' from a command prompt.            Try                CommandObj = applicationObject.Commands.AddNamedCommand(objAddIn, &quot;InsertDateTime&quot;, &quot;InsertDateTime&quot;, &quot;Executes the command for InsertDateTime&quot;, True, 59, Nothing, 1 + 2)      '1+2 == vsCommandStatusSupported+vsCommandStatusEnabled                CommandObj.AddControl(applicationObject.CommandBars.Item(&quot;Tools&quot;))            Catch e As System.Exception            End Try        End If    End Sub

Public Sub Exec(ByVal cmdName As String, ByVal executeOption As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) Implements IDTCommandTarget.Exec handled = False If (executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault) Then If cmdName = &quot;InsertDateTime.Connect.InsertDateTime&quot; Then handled = InsertDateTime Exit Sub End If       End If    End Sub

Public Function InsertDateTime As Boolean If Not IsNothing(applicationObject.ActiveDocument) Then CType(applicationObject.ActiveDocument.Selection, TextSelection).Text = DateTime.Now.ToString End If       Return True End Function

Public Sub QueryStatus(ByVal cmdName As String, ByVal neededText As vsCommandStatusTextWanted, ByRef statusOption As vsCommandStatus, ByRef commandText As Object) Implements IDTCommandTarget.QueryStatus If neededText = EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then If cmdName = &quot;InsertDateTime.Connect.InsertDateTime&quot; Then statusOption = CType(vsCommandStatus.vsCommandStatusEnabled + vsCommandStatus.vsCommandStatusSupported, vsCommandStatus) Else statusOption = vsCommandStatus.vsCommandStatusUnsupported End If       End If    End Sub End Class back to the top

Verify That It Works

 * 1) Press the CTRL+N key combination.
 * 2) In the New File dialog box, click General under Categories, click Text File under Templates, and then click Open.
 * 3) On the Tools menu, click Insert Date and Time. The current date and time are written to the text file.

back to the top

Uninstall the Add-in
<ol> <li>Close all instances of the Visual Studio .NET environment.</li> <li>Open Windows Explorer, and then browse to the following folder:

C:\InsertDateTime\InsertDateTimeSetup\Debug

</li> <li>Right-click InsertDateTimeSetup.msi, and then click Uninstall.</li> <li>Click Yes when you are prompted to uninstall.</li></ol>

back to the top

Troubleshooting
<ul> <li>Add-ins are compiled, and entries written to the registry. Therefore, any time you change the add-in project, you must recompile and reinstall the add-in.</li> <li>When you try to build the project, you may receive one of the following error messages:

unable to write to output file

-or-

file in use

To resolve this problem, right-click the setup project in Solution Explorer, and then click Uninstall to uninstall the add-in. You can then rebuild both projects and reinstall the add-in.</li></ul>

back to the top