2012-04-14 20 views
2

Visual Studio 2010を使用してExcelアドインを作成しています。選択された1つまたは複数のセルに対して何らかのアクションを実行する。ここで私は、このアドインでは、コンテキストメニューをインストールすると、私は今Visual Studio 2010で作成したExcelアドインを使用してExcelで作成したカスタムコンテキストメニューをクリックしてアクションを実行する方法

Public Class CC 

    Private Sub ThisAddIn_Startup() Handles Me.Startup 
     AddMenu() 
    End Sub 

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown 
     DeleteMenu() 
    End Sub 

    'AddMenu add context menu to excel 
    Sub AddMenu() 
     On Error Resume Next 
     Dim Bar As Microsoft.Office.Core.CommandBar 
     Dim NewControl As Microsoft.Office.Core.CommandBarControl 
     Application.CommandBars("Cell").Controls("A").Delete() 
     Bar = Application.CommandBars("Cell") 
     NewControl = Bar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, Id:=1, Temporary:=True) 

     With NewControl 
      .Caption = "A" 
      .BeginGroup = True 
      .TooltipText = "Change case of selected cells." 
     End With 

     With NewControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton) 
      .Caption = "A1" 
      .FaceId = 1144 
      .OnAction = "A1" 
     End With 

     With NewControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton) 
      .Caption = "A2" 
      .FaceId = 1145 
      .OnAction = "A2" 
     End With 

     With NewControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton) 
      .Caption = "A3" 
      .FaceId = 1155 
      .OnAction = "A3" 
     End With 

    End Sub 

    'DeleteMenu deletes the context meny added to excel 
    Sub DeleteMenu() 
     On Error Resume Next 
     Application.CommandBars("Cell").Controls("A").Delete() 
    End Sub 

    Sub A1() 
     MsgBox "A1" 
    End Sub 

    Sub A2() 
     MsgBox "A2" 
    End Sub 

    Sub A3() 
     MsgBox "A3" 
    End Sub 

End Class 

のよう持っているコードは、Excelで表示されますが、私はメニューボタンをクリックしたとき、私はマクロが利用できないというエラーを取得していますブックで。誰でも私にそれを動作させる方法を教えてもらえますか?

答えて

5

あなたのメソッドA1、A2およびA3は、Excelにマクロとして自動的に登録されません。結果としてボタンのOnAction文字列に名前を設定することは効果がありません。Excelは "A1"と呼ばれるマクロについてはわかりません。したがって、この意味では、VSTOアドインはVBAのコードのように動作しません。

ただし、CommandBarボタンの場合は、WithEventsキーワードを使用してボタンのClickイベントを処理するイベントハンドラを追加できます。 http://msdn.microsoft.com/en-us/library/aa189726(v=office.10).aspx

Excel-DNA(私が開発したオープンソースの.NET/Excel統合ライブラリ)を使用すると、.NETコードのメソッドとユーザー定義関数がExcelでC言語で登録されます。 API。その結果、動作はVBAの動作に近く、OnAction = "..."文字列のコードも機能します。

+0

これを同様のCommandBarButton プライベートmFHDefDict私にとっては難しいです。私はビジュアルスタジオには初めてです。コントロールボタンでもクリックを処理する方法について説明してください。 – Ian

+0

どのバージョンのOfficeをターゲットにしていますか? Office 2003/2007の場合は、ここでの作業用に表示されているような操作を行うことができます。http://msdn.microsoft.com/en-us/library/47cs5caa(VS.80).aspx、Office 2010の場合は、リボンxmlはここのように:http://stackoverflow.com/questions/6652837/how-to-have-a-vsto-ribbon-and-context-menu-at-the-same-time – Govert

+0

最後に私はそれを管理しました。あなたは私が試してみるのに役立ったのです。ありがとう。 – Ian

0
Public Class CC 

Private WithEvents A1 As Office.CommandBarButton 
Private WithEvents A2 As Office.CommandBarButton 
Private WithEvents A3 As Office.CommandBarButton 

Private Sub ThisAddIn_Startup() Handles Me.Startup 
    AddMenu() 
End Sub 

Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown 
    DeleteMenu() 
End Sub 

'AddMenu add context menu to excel 
Sub AddMenu() 
    On Error Resume Next 
    Dim Bar As Microsoft.Office.Core.CommandBar 
    Dim NewControl As Microsoft.Office.Core.CommandBarControl 
    Application.CommandBars("Cell").Controls("A").Delete() 
    Bar = Application.CommandBars("Cell") 
    NewControl = Bar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, Id:=1, Temporary:=True) 

    With NewControl 
     .Caption = "A" 
     .BeginGroup = True 
     .TooltipText = "Change case of selected cells." 
    End With 

    A1 = NewControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton) 

    With NewControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton) 
     .Caption = "A1" 
     .FaceId = 1144 
    End With 

    A2 = NewControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton) 

    With NewControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton) 
     .Caption = "A2" 
     .FaceId = 1145 
    End With 

    A3 = NewControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton) 

    With NewControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton) 
     .Caption = "A3" 
     .FaceId = 1155 
    End With 

End Sub 

'DeleteMenu deletes the context meny added to excel 
Sub DeleteMenu() 
    On Error Resume Next 
    Application.CommandBars("Cell").Controls("A").Delete() 
End Sub 

Sub A1() 
    MsgBox "A1" 
End Sub 

Sub A2() 
    MsgBox "A2" 
End Sub 

Sub A3() 
    MsgBox "A3" 
End Sub 

Private Sub A1_Click(ByVal Ctrl As Office.CommandBarButton, ByRef CancelDefault As Boolean) Handles A1.Click 
    A1() 
End Sub 

Private Sub A2_Click(ByVal Ctrl As Office.CommandBarButton, ByRef CancelDefault As Boolean) Handles A2.Click 
    A2() 
End Sub 

Private Sub A3_Click(ByVal Ctrl As Office.CommandBarButton, ByRef CancelDefault As Boolean) Handles A3.Click 
    A3() 
End Sub 
End Class 

これは、私は上記の問題

0

これが問題のクマですが見つかりソリューションです。非常に小さな情報を作成する上でどこでも、VB.NETのポップアップをExcelします。これは半ダイナミックメニューを作成する私のバージョンです。メニュー項目はこの場合の辞書から来ますが、どこから来てもかまいません。このクラスを作成し、ワークブックの起動イベントにロードしました。 Microsoft.Office.CoreとしてMicrosoft.Office.Core.CommandBarPopup プライベートWITHEVENTS tagFH1としてMicrosoft.Office.Core.CommandBarPopup プライベートmCmdBarPopRHとしてMicrosoft.Office.Core.CommandBarPopup プライベートmCmdBarPopPHとして

パブリック・クラスポップアップ プライベートmCmdBarPopFH Microsoft.Office.Core.CommandBarButtonとしてMicrosoft.Office.Core.CommandBarButton プライベートWITHEVENTS tagPH2としてMicrosoft.Office.Core.CommandBarButton プライベートWITHEVENTS tagPH1としてMicrosoft.Office.Core.CommandBarButton プライベートWITHEVENTS tagFH3として.CommandBarButton プライベートWITHEVENTS tagFH2 非公開WithEventsタグPH3としてMicros Microsoft.Office.Core.CommandBarButton としてMicrosoft.Office.Core.CommandBarButton プライベートWITHEVENTS tagRH3としてMicrosoft.Office.Core.CommandBarButton プライベートWITHEVENTS tagRH2としてoft.Office.Core.CommandBarButton プライベートWITHEVENTS tagRH1プライベートWITHEVENTSは、マイクロソフトのようtag1st。 Office.Core.CommandBarButton Private WithEvents tag2nd As Microsoft.Office.Core.CommandBarButton プライベートWithEvents tagClrとしては、Microsoft.Office.Coreです。(文字列のうち、HeaderDef)新しい辞書新しい辞書として プライベートmPHDefDict(文字列のうち、HeaderDef)新しい辞書として プライベートmRHDefDict(文字列のうち、HeaderDef)

Private mPHSheet As Excel.Worksheet 'temp until sheet management 
Private mRHSheet As Excel.Worksheet 
Private mFHSheet As Excel.Worksheet 

'************************************************************************************ 
'Add popup menu for marking sample file. 
'************************************************************************************ 
Public Sub TagsMenuAdd() 
    Dim oHeaderDefs As New HeaderDefs 
    Dim oCmdBar As Microsoft.Office.Core.CommandBar 

    mFHSheet = CType(Globals.ThisWorkbook.Application.Sheets("File Headers"), Excel.Worksheet) 
    mPHSheet = CType(Globals.ThisWorkbook.Application.Sheets("Plate Headers"), Excel.Worksheet) 
    mRHSheet = CType(Globals.ThisWorkbook.Application.Sheets("Read Headers"), Excel.Worksheet) 
    mFHDefDict = oHeaderDefs.DefDictLoad(mFHSheet) 'temp until sheet management 
    mPHDefDict = oHeaderDefs.DefDictLoad(mPHSheet) 
    mRHDefDict = oHeaderDefs.DefDictLoad(mRHSheet) 

    oCmdBar = Globals.ThisWorkbook.Application.CommandBars.Add(Name:="Fil_CellMarking", Position:=Microsoft.Office.Core.MsoBarPosition.msoBarPopup, Temporary:=True) 
    With oCmdBar 
     tag1st = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
     tag1st.Caption = "Mark 1st Well of 1st data set" 
     tag1st.Tag = "1st" 
     tag2nd = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
     tag2nd.Caption = "Mark 1st Well of 2nd data set" 
     tag2nd.Tag = "2nd" 
     mCmdBarPopFH = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlPopup), Microsoft.Office.Core.CommandBarPopup) 
     With mCmdBarPopFH 
      .Caption = "Mark File Headers" 
      .Enabled = True 
     End With 
     mCmdBarPopPH = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlPopup), Microsoft.Office.Core.CommandBarPopup) 
     With mCmdBarPopPH 
      .Caption = "Mark Plate Headers" 
      .Enabled = True 
     End With 
     mCmdBarPopRH = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlPopup), Microsoft.Office.Core.CommandBarPopup) 
     With mCmdBarPopRH 
      .Caption = "Mark Read Headers" 
      .Enabled = True 
     End With 
     tagClr = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
     tagClr.Caption = "Clear All Markings" 
     tagClr.Tag = "clr" 
    End With 

    TagsMenuItemsFH(mFHDefDict) 
    TagsMenuItemsPH(mPHDefDict) 
    TagsMenuItemsRH(mRHDefDict) 
End Sub 
'************************************************************************************ 
'Add popup menu items for marking sample file. 
'************************************************************************************ 
Public Sub TagsMenuItemsFH(DefDict As Dictionary(Of String, HeaderDef)) 
    Dim iButtons As Integer 
    iButtons = 1 
    For Each sKey As String In DefDict.Keys 
     Select Case iButtons 
      Case 1 
       With mCmdBarPopFH 
        tagFH1 = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
        tagFH1.Caption = DefDict(sKey).HeaderName 
        tagFH1.Tag = "FH1" 
       End With 
      Case 2 
       With mCmdBarPopFH 
        tagFH2 = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
        tagFH2.Caption = DefDict(sKey).HeaderName 
        tagFH2.Tag = "FH2" 
       End With 
      Case 3 
       With mCmdBarPopFH 
        tagFH3 = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
        tagFH3.Caption = DefDict(sKey).HeaderName 
        tagFH3.Tag = "FH3" 
       End With 
     End Select 
     iButtons = iButtons + 1 
    Next 
End Sub 
Public Sub TagsMenuItemsPH(DefDict As Dictionary(Of String, HeaderDef)) 
    Dim iButtons As Integer 
    iButtons = 1 
    For Each sKey As String In DefDict.Keys 
     With mCmdBarPopPH 
     Select iButtons 
       Case 1 
        tagPH1 = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
        tagPH1.Caption = DefDict(sKey).HeaderName 
        tagPH1.Tag = "PH1" 
       Case 2 
        tagPH2 = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
        tagPH2.Caption = DefDict(sKey).HeaderName 
        tagPH2.Tag = "PH2" 
       Case 3 
        tagPH3 = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
        tagPH3.Caption = DefDict(sKey).HeaderName 
        tagPH3.Tag = "PH3" 
      End Select 
     End With 
     iButtons = iButtons + 1 
    Next 
End Sub 
Public Sub TagsMenuItemsRH(DefDict As Dictionary(Of String, HeaderDef)) 
    Dim iButtons As Integer 
    iButtons = 1 
    For Each sKey As String In DefDict.Keys 
     With mCmdBarPopRH 
      Select Case iButtons 
       Case 1 
        tagRH1 = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
        tagRH1.Caption = DefDict(sKey).HeaderName 
        tagRH1.Tag = "RH1" 
       Case 2 
        tagRH2 = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
        tagRH2.Caption = DefDict(sKey).HeaderName 
        tagRH2.Tag = "RH2" 
       Case 3 
        tagRH3 = CType(.Controls.Add(Type:=Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton) 
        tagRH3.Caption = DefDict(sKey).HeaderName 
        tagRH3.Tag = "RH3" 
      End Select 
     End With 
     iButtons = iButtons + 1 
    Next 
End Sub 
Private Sub Button_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles tag1st.Click, tag2nd.Click, tagClr.Click 
    Select Case Ctrl.Tag 
     Case "1st" 
      MsgBox("1st") 
     Case "2nd" 
      MsgBox("2nd") 
     Case "clr" 
      MsgBox("clr") 
    End Select 
End Sub 
Private Sub Header_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles tagFH1.Click, tagFH2.Click, tagFH3.Click, tagPH1.Click, tagPH2.Click, tagPH3.Click, tagRH1.Click, tagRH2.Click, tagRH3.Click 
    Select Case Ctrl.Tag 
     Case "FH1" 
      MsgBox("FH1") 
     Case "FH2" 
      MsgBox("FH2") 
     Case "FH3" 
      MsgBox("FH3") 
     Case "PH1" 
      MsgBox("PH1") 
     Case "PH2" 
      MsgBox("PH2") 
     Case "PH3" 
      MsgBox("PH3") 
     Case "RH1" 
      MsgBox("RH1") 
     Case "RH2" 
      MsgBox("RH2") 
     Case "RH3" 
      MsgBox("RH3") 
    End Select 
End Sub 

エンドクラス

関連する問題