Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 5964|回复: 3

[分享] VSTO探索笔记之右键菜单及ListObject对象事件响应

[复制链接]
发表于 2014-4-27 21:12 | 显示全部楼层 |阅读模式
本帖最后由 sanbe 于 2014-4-27 21:20 编辑

windows7 64bit+excel 2010+vs2013
windows7 64bit+excel2010+vs2010测试通过

添加右键菜单代码

  1. Dim combar As Microsoft.Office.Core.CommandBar = activeWorksheet.Application.CommandBars("Cell")'右键菜单
  2. DelRightMenu("List Range Popup", "test")'删除右建菜单
  3. Dim mycol As Microsoft.Office.Core.CommandBarControl =combar.Controls.Ad(Microsoft.Office.Core.MsoControlType.msoControlButton)
  4. Dim comBut As Microsoft.Office.Core.CommandBarButton = TryCast(mycol, Microsoft.Office.Core.CommandBarButton)
  5. If Not IsNothing(comBut) Then
  6.     comBut.Tag = "test"
  7.     comBut.Caption = "测试"
  8.     comBut.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonAutomatic
  9.     AddHandler comBut.Click, AddressOf comBut_Click'事件委托
  10. End If
复制代码
删除右键菜单过程

  1.     Public Shared Sub DelRightMenu(ByVal BarName As String, ByVal Tag As String, Optional ByVal Visible As Boolean = True)
  2.         Dim bars As Microsoft.Office.Core.CommandBarControls = Globals.ThisAddIn.Application.CommandBars(BarName).Controls
  3.         For Each col As Microsoft.Office.Core.CommandBarControl In bars
  4.             If col.Tag = Tag Then
  5.                 col.Delete()
  6.             Else
  7.                 col.Visible = Visible
  8.             End If
  9.         Next
  10.     End Sub
复制代码
菜单单击事件

  1.     Public Shared Sub comBut_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
  2.         MsgBox("测试")
  3.     End Sub
复制代码
以下内容没找到最佳处理方法,采用VB.net的事件委托方法
ListObject右键单击事件委托

  1. Public Shared WithEvents myListObjet As Microsoft.Office.Interop.Excel.ListObject'公共声明ListObject响应事件
  2. '以下委托放到其他过程中
  3. AddHandler myOrderListObjet.Application.SheetBeforeRightClick, AddressOf Worksheet_myListObjet_BeforeRightClick'委托右键事件

复制代码
ListObject右键单击事件,添加右建菜单

  1. Public Sub Worksheet_myListObjet_BeforeRightClick(ByVal sh As Object, ByVal Target As Microsoft.Office.Interop.Excel.Range, ByRef Cancel As Boolean)
  2.     Dim combar As Microsoft.Office.Core.CommandBar = activeWorksheet.Application.CommandBars("List Range Popup")'ListObject对象菜单
  3.     DelRightMenu("List Range Popup", "test")'删除ListObject对象右建菜单
  4.     Dim mycol As Microsoft.Office.Core.CommandBarControl =combar.Controls.Ad(Microsoft.Office.Core.MsoControlType.msoControlButton)
  5.     Dim comBut As Microsoft.Office.Core.CommandBarButton = TryCast(mycol, Microsoft.Office.Core.CommandBarButton)
  6.     If Not IsNothing(comBut) Then
  7.         comBut.Tag = "test"
  8.         comBut.Caption = "测试"
  9.         comBut.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonAutomatic
  10.         AddHandler comBut.Click, AddressOf comBut_Click'事件委托
  11.     End If
  12. End Sub
复制代码
其它笔记链接:VSTO探索笔记

评分

参与人数 3 +67 金币 +54 收起 理由
顺⑦.zì繎。 + 30 + 30 赞一个!
冥王 + 15
业余爱好者 + 22 + 24 很给力!

查看全部评分

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-4-27 21:20 | 显示全部楼层
回复

使用道具 举报

发表于 2014-4-27 23:45 | 显示全部楼层
回复

使用道具 举报

发表于 2019-2-8 09:40 | 显示全部楼层
AddHandler myOrderListObjet  出错
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 05:00 , Processed in 0.122603 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表