这是一个问题。在 vb.net 中创建 excel 弹出窗口的任何信息都很少。这是我创建半动态菜单的版本。在这种情况下,菜单项来自字典,但可能来自任何地方。创建了此类并将其加载到工作簿启动事件中。
公共类 Popups Private mCmdBarPopFH As Microsoft.Office.Core.CommandBarPopup Private mCmdBarPopPH As Microsoft.Office.Core.CommandBarPopup Private mCmdBarPopRH As Microsoft.Office.Core.CommandBarPopup Private WithEvents tagFH1 As Microsoft.Office.Core.CommandBarButton Private WithEvents tagFH2 As Microsoft。 Office.Core.CommandBarButton Private WithEvents tagFH3 As Microsoft.Office.Core.CommandBarButton Private WithEvents tagPH1 As Microsoft.Office.Core.CommandBarButton Private WithEvents tagPH2 As Microsoft.Office.Core.CommandBarButton Private WithEvents tagPH3 As Microsoft.Office.Core.CommandBarButton Private WithEvents tagRH1 作为 Microsoft.Office.Core.CommandBarButton 私有 WithEvents tagRH2 作为 Microsoft.Office.Core.CommandBarButton 私有 WithEvents tagRH3 作为 Microsoft.Office.Core。CommandBarButton Private WithEvents tag1st As Microsoft.Office.Core.CommandBarButton Private WithEvents tag2nd As Microsoft.Office.Core.CommandBarButton Private WithEvents tagClr As Microsoft.Office.Core.CommandBarButton Private mFHDefDict As New Dictionary(Of String, HeaderDef) Private mPHDefDict As New Dictionary (字符串,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
结束类