|
本帖最后由 sanbe 于 2014-4-27 21:20 编辑
windows7 64bit+excel 2010+vs2013
windows7 64bit+excel2010+vs2010测试通过
添加右键菜单代码
- Dim combar As Microsoft.Office.Core.CommandBar = activeWorksheet.Application.CommandBars("Cell")'右键菜单
- DelRightMenu("List Range Popup", "test")'删除右建菜单
- Dim mycol As Microsoft.Office.Core.CommandBarControl =combar.Controls.Ad(Microsoft.Office.Core.MsoControlType.msoControlButton)
- Dim comBut As Microsoft.Office.Core.CommandBarButton = TryCast(mycol, Microsoft.Office.Core.CommandBarButton)
- If Not IsNothing(comBut) Then
- comBut.Tag = "test"
- comBut.Caption = "测试"
- comBut.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonAutomatic
- AddHandler comBut.Click, AddressOf comBut_Click'事件委托
- End If
复制代码 删除右键菜单过程
- Public Shared Sub DelRightMenu(ByVal BarName As String, ByVal Tag As String, Optional ByVal Visible As Boolean = True)
- Dim bars As Microsoft.Office.Core.CommandBarControls = Globals.ThisAddIn.Application.CommandBars(BarName).Controls
- For Each col As Microsoft.Office.Core.CommandBarControl In bars
- If col.Tag = Tag Then
- col.Delete()
- Else
- col.Visible = Visible
- End If
- Next
- End Sub
复制代码 菜单单击事件
- Public Shared Sub comBut_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
- MsgBox("测试")
- End Sub
复制代码 以下内容没找到最佳处理方法,采用VB.net的事件委托方法
ListObject右键单击事件委托
- Public Shared WithEvents myListObjet As Microsoft.Office.Interop.Excel.ListObject'公共声明ListObject响应事件
- '以下委托放到其他过程中
- AddHandler myOrderListObjet.Application.SheetBeforeRightClick, AddressOf Worksheet_myListObjet_BeforeRightClick'委托右键事件
复制代码 ListObject右键单击事件,添加右建菜单
- Public Sub Worksheet_myListObjet_BeforeRightClick(ByVal sh As Object, ByVal Target As Microsoft.Office.Interop.Excel.Range, ByRef Cancel As Boolean)
- Dim combar As Microsoft.Office.Core.CommandBar = activeWorksheet.Application.CommandBars("List Range Popup")'ListObject对象菜单
- DelRightMenu("List Range Popup", "test")'删除ListObject对象右建菜单
- Dim mycol As Microsoft.Office.Core.CommandBarControl =combar.Controls.Ad(Microsoft.Office.Core.MsoControlType.msoControlButton)
- Dim comBut As Microsoft.Office.Core.CommandBarButton = TryCast(mycol, Microsoft.Office.Core.CommandBarButton)
- If Not IsNothing(comBut) Then
- comBut.Tag = "test"
- comBut.Caption = "测试"
- comBut.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonAutomatic
- AddHandler comBut.Click, AddressOf comBut_Click'事件委托
- End If
- End Sub
复制代码 其它笔记链接:VSTO探索笔记
|
评分
-
查看全部评分
|