i got most of this code from a link given to me in a previous thread. i think i understand it + i'm surprised it doesn't work. the addin doesn't do anything yet, i'm still working on adding an edit menuitem + an edit contextmenuitem. thats the part that doesn't work. i'll be grateful for any help. thanks.

vb.net Code:
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.IO
  4. Imports System.Runtime.InteropServices
  5. Imports System.Web
  6. Imports System.Windows.Forms
  7. Imports Microsoft.VisualStudio.CommandBars
  8. Imports Extensibility
  9. Imports EnvDTE
  10. Imports EnvDTE80
  11.  
  12. Public Class Connect
  13.  
  14.     Implements IDTExtensibility2
  15.     Implements IDTCommandTarget
  16.  
  17.     Dim _applicationObject As DTE2
  18.     Dim _addInInstance As AddIn
  19.  
  20.     Private controls As List(Of CommandBarControl)
  21.     Private pasteCommand As Command
  22.     Private Shared contextGuids As Object() = New Object() {}
  23.     Private Shared contextMenuNames As String() = New String() {"Code Window"}
  24.  
  25.     '''<summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
  26.     Public Sub New()
  27.         Me.controls = New List(Of CommandBarControl)()
  28.     End Sub
  29.  
  30.     '''<summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
  31.     '''<param name='application'>Root object of the host application.</param>
  32.     '''<param name='connectMode'>Describes how the Add-in is being loaded.</param>
  33.     '''<param name='addInInst'>Object representing this Add-in.</param>
  34.     '''<remarks></remarks>
  35.     Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef custom As Array) Implements IDTExtensibility2.OnConnection
  36.         _applicationObject = CType(application, DTE2)
  37.         _addInInstance = CType(addInInst, AddIn)
  38.  
  39.         DeleteControls()
  40.         AddCommands()
  41.         AddControls()
  42.  
  43.     End Sub
  44.  
  45.     '''<summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
  46.     '''<param name='disconnectMode'>Describes how the Add-in is being unloaded.</param>
  47.     '''<param name='custom'>Array of parameters that are host application specific.</param>
  48.     '''<remarks></remarks>
  49.     Public Sub OnDisconnection(ByVal disconnectMode As ext_DisconnectMode, ByRef custom As Array) Implements IDTExtensibility2.OnDisconnection
  50.         DeleteControls()
  51.     End Sub
  52.  
  53.     '''<summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification that the collection of Add-ins has changed.</summary>
  54.     '''<param name='custom'>Array of parameters that are host application specific.</param>
  55.     '''<remarks></remarks>
  56.     Public Sub OnAddInsUpdate(ByRef custom As Array) Implements IDTExtensibility2.OnAddInsUpdate
  57.     End Sub
  58.  
  59.     '''<summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
  60.     '''<param name='custom'>Array of parameters that are host application specific.</param>
  61.     '''<remarks></remarks>
  62.     Public Sub OnStartupComplete(ByRef custom As Array) Implements IDTExtensibility2.OnStartupComplete
  63.     End Sub
  64.  
  65.     '''<summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
  66.     '''<param name='custom'>Array of parameters that are host application specific.</param>
  67.     '''<remarks></remarks>
  68.     Public Sub OnBeginShutdown(ByRef custom As Array) Implements IDTExtensibility2.OnBeginShutdown
  69.     End Sub
  70.  
  71.     Public Sub QueryStatus(ByVal commandName As String, ByVal neededText As vsCommandStatusTextWanted, ByRef status As vsCommandStatus, _
  72.     ByRef commandText As Object) Implements IDTCommandTarget.QueryStatus
  73.  
  74.         Try
  75.             If neededText = vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then
  76.                 If Me._applicationObject.ActiveDocument Is Nothing Then
  77.                     status = vsCommandStatus.vsCommandStatusUnsupported
  78.                 ElseIf (Me._applicationObject.ActiveDocument.Selection Is Nothing) OrElse DirectCast(Me._applicationObject.ActiveDocument.Selection, TextSelection).IsEmpty Then
  79.                     status = vsCommandStatus.vsCommandStatusSupported
  80.                 Else
  81.                     status = DirectCast((vsCommandStatus.vsCommandStatusSupported Or vsCommandStatus.vsCommandStatusEnabled), vsCommandStatus)
  82.                 End If
  83.             End If
  84.         Catch e As Exception
  85.             'MessageBox.Show([String].Format(Resources.CaughtExceptionMessage, e), Resources.CaughtExceptionCaption, MessageBoxButtons.OK, MessageBoxIcon.[Error])
  86.         End Try
  87.  
  88.     End Sub
  89.  
  90.     Public Sub Exec(ByVal commandName As String, ByVal executeOption As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, _
  91.     ByRef handled As Boolean) Implements IDTCommandTarget.Exec
  92.         Try
  93.             handled = False
  94.             If executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault Then
  95.  
  96.                 'Copy(False)
  97.                 'paste vbcode here
  98.                 Dim rtb As New RichTextBox
  99.                 rtb.Paste()
  100.                 rtb.SelectAll()
  101.                 rtb.Cut()
  102.                 ' TODO: paste clipboard contents into code editor
  103.                 handled = True
  104.  
  105.             End If
  106.         Catch e As Exception
  107.             'MessageBox.Show([String].Format(Resources.CaughtExceptionMessage, e), Resources.CaughtExceptionCaption, MessageBoxButtons.OK, MessageBoxIcon.[Error])
  108.         End Try
  109.  
  110.     End Sub
  111.  
  112.     Private Sub AddCommands()
  113.         If Me.pasteCommand Is Nothing Then
  114.             Me.pasteCommand = AddCommand("Paste_VBCode.connect.Paste_VBCode", "Paste_VBCode", "Paste VBCode", String.Empty, 0)
  115.         End If
  116.     End Sub
  117.  
  118.     Private Function AddCommand(ByVal fullCommandName As String, ByVal partialCommandName As String, ByVal buttonText As String, ByVal tooltip As String, ByVal icon As Integer) As Command
  119.  
  120.         Dim command As Command
  121.  
  122.         command = FindCommand(fullCommandName)
  123.         If command Is Nothing Then
  124.             command = DirectCast(Me._applicationObject.Commands, Commands2).AddNamedCommand2(Me._addInInstance, partialCommandName, buttonText, tooltip, True, icon, _
  125.              contextGuids, CInt(vsCommandStatus.vsCommandStatusSupported) + CInt(vsCommandStatus.vsCommandStatusEnabled), CInt(vsCommandStyle.vsCommandStyleText), vsCommandControlType.vsCommandControlTypeButton)
  126.         End If
  127.         Return command
  128.  
  129.     End Function
  130.  
  131.     Private Sub AddControls()
  132.  
  133.         Dim commandBars As CommandBars
  134.         Dim editPopup As CommandBarPopup
  135.         Dim copyIndex As Integer
  136.  
  137.         commandBars = DirectCast(Me._applicationObject.CommandBars, CommandBars)
  138.         editPopup = DirectCast(commandBars("MenuBar").Controls("Edit"), CommandBarPopup)
  139.  
  140.         copyIndex = FindPasteOnCommandBar("Edit") + 1
  141.  
  142.         Me.controls.Add(DirectCast(Me.pasteCommand.AddControl(editPopup.CommandBar, copyIndex), CommandBarControl))
  143.  
  144.         For Each contextMenuName As String In contextMenuNames
  145.             copyIndex = FindPasteOnCommandBar(contextMenuName) + 1
  146.             Me.controls.Add(DirectCast(Me.pasteCommand.AddControl(commandBars(contextMenuName), copyIndex), CommandBarControl))
  147.         Next
  148.  
  149.     End Sub
  150.  
  151.     Private Sub DeleteControls()
  152.         If Me.controls.Count > 0 Then
  153.             For Each control As CommandBarControl In Me.controls
  154.                 Try
  155.                     control.Delete(False)
  156.                 Catch
  157.                 End Try
  158.             Next
  159.             Me.controls.Clear()
  160.         End If
  161.     End Sub
  162.  
  163.     Private Function FindCommand(ByVal name As String) As Command
  164.         For Each command As Command In DirectCast(Me._applicationObject.Commands, Commands2)
  165.             If command.Name = name Then
  166.                 Return command
  167.             End If
  168.         Next
  169.         Return Nothing
  170.     End Function
  171.  
  172.     Private Function FindPasteOnCommandBar(ByVal commandBarName As String) As Integer
  173.  
  174.         Dim commandBars As CommandBars
  175.         Dim index As Integer
  176.  
  177.         commandBars = DirectCast(Me._applicationObject.CommandBars, CommandBars)
  178.         For index = 1 To commandBars(commandBarName).Controls.Count
  179.             If commandBars(commandBarName).Controls(index).Caption.Replace("&", "").Trim = "Paste" Then
  180.                 Return index
  181.             End If
  182.         Next
  183.         Return 0
  184.  
  185.     End Function
  186.  
  187. End Class