2017-05-10 51 views
2

我在VBA代碼(在一個簡化版本所示):VB.NET當量的CommandBars

Sub TheMenu() 
    Dim Obj As CommandBar 
    Set Obj = Application.CommandBars.Add(Position:=msoBarPopup, MenuBar:=False, Temporary:=True) 
    Obj.Controls.Add(Type:=msoControlButton).Caption = "Button1" 
    Obj.Controls.Add(Type:=msoControlButton).Caption = "Button2" 
    Obj.ShowPopup 
End Sub 

我希望做的東西當量(意爲「看起來相似,也有類似的用途」,我不在VB.NET中不需要更多)。你知道這樣做的任何方式嗎? Excel VBA rendering

我在使用.NET Framework 4.6.1的「Windows Forms應用程序」項目VS2015中使用。

+0

我已經auccessfully使用幾乎完全與VSTO,除了語法的變化相同。也就是說沒有'Set','Type:= MsoControlType.msoControlButton'等等......(當然我使用了Controls.Add(1)'我不記得是否有特定的原因)。 –

+0

所有這些類型在我的視覺工作室中都是未知的...您使用了什麼導入? – Pierre

+0

'Microsoft.Office.Core','Microsoft.Office.Interop','Microsoft.Office.Interop.Excel'。但是該項目是通過VSTO創建的,所以IDR如果自動添加。早在幾年前就已經完成了,但自從「回」到VBA :)。另外我在項目中看到'Microsoft.Office.Tools.Common','Microsoft.Office.Tools.Excel'和其他一些東西,我想VSTO在項目創建爲Workbook項目後添加了它們。 –

回答

0

可以效仿的榜樣婁這裏:

Public Class Connect 
    Implements Extensibility.IDTExtensibility2 
    Implements IDTCommandTarget 

    Private Const MY_COMMAND_NAME As String = "MyCommand" 

    Private applicationObject As EnvDTE.DTE 
    Private addInInstance As EnvDTE.AddIn 
    Private myStandardCommandBarControl As CommandBarControl 
    Private myToolsCommandBarControl As CommandBarControl 
    Private myCodeWindowCommandBarControl As CommandBarControl 
    Private myTemporaryToolbar As CommandBar 
    Private myTemporaryCommandBarPopup As CommandBarPopup 

    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 

     Dim myCommand As Command 
     Dim standardCommandBar As CommandBar 
     Dim menuCommandBar As CommandBar 
     Dim toolsCommandBar As CommandBar 
     Dim codeCommandBar As CommandBar 
     Dim toolsCommandBarControl As CommandBarControl 
     Dim myCommandBarButton As CommandBarButton 
     Dim position As Integer 

     Try 

     applicationObject = CType(application, EnvDTE.DTE) 
     addInInstance = CType(addInInst, EnvDTE.AddIn) 

     Select Case connectMode 

      Case ext_ConnectMode.ext_cm_AfterStartup, ext_ConnectMode.ext_cm_Startup 

       ' Try to retrieve the command, just in case it was already created 
       Try 
        myCommand = applicationObject.Commands.Item(addInInstance.ProgID & "." & "MyCommand") 
       Catch 
       End Try 

       ' Add the command if it does not exists 
       If myCommand Is Nothing Then 

        myCommand = applicationObject.Commands.AddNamedCommand(addInInstance, _ 
        "MyCommand", "MyCommand", "Executes the command for MyAddin", True, 59, Nothing, _ 
        vsCommandStatus.vsCommandStatusSupported Or vsCommandStatus.vsCommandStatusEnabled) 

       End If 

       ' Retrieve some built-in command bars 
       standardCommandBar = applicationObject.CommandBars.Item("Standard") 
       menuCommandBar = applicationObject.CommandBars.Item("MenuBar") 
       toolsCommandBar = applicationObject.CommandBars.Item("Tools") 
       codeCommandBar = applicationObject.CommandBars.Item("Code Window") 

       ' Add a button to the built-in "Standard" toolbar 
       myStandardCommandBarControl = myCommand.AddControl(standardCommandBar, _ 
        standardCommandBar.Controls.Count + 1) 

       myStandardCommandBarControl.Caption = MY_COMMAND_NAME 

       ' Change the button style, which must be done casting the control to a button 
       myCommandBarButton = DirectCast(myStandardCommandBarControl, CommandBarButton) 
       myCommandBarButton.Style = MsoButtonStyle.msoButtonIcon 

       ' Add a button to the built-in "Tools" menu 
       myToolsCommandBarControl = myCommand.AddControl(toolsCommandBar, toolsCommandBar.Controls.Count + 1) 
       myToolsCommandBarControl.Caption = MY_COMMAND_NAME 

       ' Add a button to the built-in "Code Window" context menu 
       myCodeWindowCommandBarControl = myCommand.AddControl(codeCommandBar, codeCommandBar.Controls.Count + 1) 
       myCodeWindowCommandBarControl.Caption = MY_COMMAND_NAME 

       ' Add a new toolbar with a button on it 
       myTemporaryToolbar = applicationObject.CommandBars.Add("MyTemporaryToolbar", _ 
        MsoBarPosition.msoBarTop, System.Type.Missing, True) 

       ' Change the button style, which must be done casting the control to a button 
       myCommandBarButton = DirectCast(myCommand.AddControl(myTemporaryToolbar), CommandBarButton) 
       myCommandBarButton.Style = MsoButtonStyle.msoButtonIcon 

       ' Make visible the toolbar 
       myTemporaryToolbar.Visible = True 

       ' Calculate the position of a new command bar popup by the "Tools" menu 
       toolsCommandBarControl = DirectCast(toolsCommandBar.Parent, CommandBarControl) 
       position = toolsCommandBarControl.Index + 1 

       ' Add a new command bar popup with a button on it 
       myTemporaryCommandBarPopup = DirectCast(menuCommandBar.Controls.Add(_ 
        MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, _ 
        position, True), CommandBarPopup) 

       myTemporaryCommandBarPopup.CommandBar.Name = "MyTemporaryCommandBarPopup" 
       myTemporaryCommandBarPopup.Caption = "My menu" 
       myCommand.AddControl(myTemporaryCommandBarPopup.CommandBar) 
       myTemporaryCommandBarPopup.Visible = True 

     End Select 

     Catch e As System.Exception 
     System.Windows.Forms.MessageBox.Show(e.ToString) 
     End Try 

    End Sub 

    Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, _ 
     ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection 

     Try 

     If Not (myStandardCommandBarControl Is Nothing) Then 
      myStandardCommandBarControl.Delete() 
     End If 

     If Not (myCodeWindowCommandBarControl Is Nothing) Then 
      myCodeWindowCommandBarControl.Delete() 
     End If 

     If Not (myToolsCommandBarControl Is Nothing) Then 
      myToolsCommandBarControl.Delete() 
     End If 

     If Not (myTemporaryToolbar Is Nothing) Then 
      myTemporaryToolbar.Delete() 
     End If 

     If Not (myTemporaryCommandBarPopup Is Nothing) Then 
      myTemporaryCommandBarPopup.Delete() 
     End If 

     Catch e As System.Exception 
     System.Windows.Forms.MessageBox.Show(e.ToString) 
     End Try 

    End Sub 

    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 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 = addInInstance.ProgID & "." & MY_COMMAND_NAME Then 

      handled = True 
      System.Windows.Forms.MessageBox.Show("Command executed.") 

     End If 

     End If 

    End Sub 

    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 = addInInstance.ProgID & "." & MY_COMMAND_NAME Then 
      statusOption = CType(vsCommandStatus.vsCommandStatusEnabled + _ 
       vsCommandStatus.vsCommandStatusSupported, vsCommandStatus) 
     Else 
      statusOption = vsCommandStatus.vsCommandStatusUnsupported 
     End If 

     End If 

    End Sub 

End Class 
+0

CommandBar在我的編輯器中不是已知的類型。也許有一個「進口」添加?謝謝! – Pierre

+0

命令欄和工具欄從Visual Studio .NET IDE加載項變爲。你在使用它嗎? –

+0

如果是這樣的答案,我使用visual studio 2015 – Pierre