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



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q317345

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 and Load Add-ins in Visual Studio .NET

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

  1. On the first page of the Extensibility Wizard, click Next.
  2. On the Select a Programming Language page, click Create an Add-in using Visual Basic, and then click Next.
  3. On the Select an Application Host page, clear the Microsoft VSMacros IDEcheck box, and then click Next.
  4. 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.
  5. 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.

  6. 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.
  7. On the Choosing 'Help About' Information page, click Next.
  8. On the Summary page, click Finish. This creates a solution with both an add-in project and an add-in setup project.
back to the top

Update the Connect.vb File and Add Custom Actions

  1. 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:
    1. Add a subroutine that writes the date and the time.
    2. Modify the Exec subroutine.
  2. 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
    					
  3. In Public Sub Exec, change the following code
    handled = True
    						
    to:
    handled = InsertDateTime()
    					
  4. In Solution Explorer, right-click InsertDateTimeSetup, point to View, and then click Custom Actions.
  5. Right-click Custom Actions, and then click Add Custom Action.
  6. Click Application Folder, and then click OK.
  7. 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.
back to the top

Build and Install the Add-in Project

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

      C:\InsertDateTime\InsertDateTimeSetup\Debug

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

      NOTE: After you restart Visual Studio .NET, the add-in is always loaded until you uninstall it.
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

#Region " Read me for Add-in installation and setup information. "
' 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.
#End Region

<GuidAttribute("0045A524-BA5F-43F4-890B-C729D9A12FDB"), ProgIdAttribute("InsertDateTime.Connect")> _
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, "InsertDateTime", "InsertDateTime", "Executes the command for InsertDateTime", True, 59, Nothing, 1 + 2)		'1+2 == vsCommandStatusSupported+vsCommandStatusEnabled
                CommandObj.AddControl(applicationObject.CommandBars.Item("Tools"))
            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 = "InsertDateTime.Connect.InsertDateTime" 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 = "InsertDateTime.Connect.InsertDateTime" 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

  1. Close all instances of the Visual Studio .NET environment.
  2. Open Windows Explorer, and then browse to the following folder:

    C:\InsertDateTime\InsertDateTimeSetup\Debug

  3. Right-click InsertDateTimeSetup.msi, and then click Uninstall.
  4. Click Yes when you are prompted to uninstall.
back to the top

Troubleshooting

  • 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.
  • 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.
back to the top

REFERENCES

For more information, refer to the following Microsoft Web sites:

Automation Samples for Visual Studio .NET
http://msdn.microsoft.com/vstudio/downloads/automation.asp

Custom Add-Ins Help You Maximize the Productivity of Visual Studio .NET
http://msdn.microsoft.com/msdnmag/issues/02/02/VSIDE/VSIDE.asp

Creating Add-Ins and Wizards
http://msdn.microsoft.com/library/en-us/vsintro7/html/vxconCreatingAutomationObjects.asp

back to the top

Modification Type:MajorLast Reviewed:3/4/2006
Keywords:kbvs2005doesnotapply kbvs2005swept kbHOWTOmaster KB317345 kbAudDeveloper