|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Imports System.Windows.Forms
- Public Class Form1
- Public col As Integer = 1
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- On Error Resume Next
- Dim xlapp As Excel.Application = CType(Globals.ThisAddIn.Application, Excel.Application) '定义EXCEL主程序
- Dim no As New TreeNode '定义
- no = Me.TreeView1.Nodes.Add("excel") '添加主node
- For Each cd As Office.CommandBar In xlapp.CommandBars '添加工作表菜单及子菜单
- addd(cd, xlapp.CommandBars, no)
- Next
- End Sub
-
- Sub add(ByVal menuitem As Object, ByVal comman As Object, ByVal nodefather As TreeNode) '递归加入工作表菜单
- Dim node As New TreeNode
- For Each menuitem In comman
- node.Text = menuitem.caption
- nodefather.Nodes.Add(node)
- Next
- For Each cmm In comman
- add(cmm, menuitem, nodefather)
- Next
- End Sub
- Sub addd(ByVal menuitem As Object, ByVal comman As Object, ByVal nodefather As TreeNode) '递归加入子菜单
- Dim node As New TreeNode
- Dim nodd As New TreeNode
- For Each menuitem In comman
- node.Text = menuitem.caption
- nodefather.Nodes.Add(node)
- For Each cmm In comman
- Select Case cmm.Type
- Case Microsoft.Office.Core.MsoControlType.msoControlPopup, Microsoft.Office.Core.MsoControlType.msoControlGraphicPopup, Microsoft.Office.Core.MsoControlType.msoControlButtonPopup, Microsoft.Office.Core.MsoControlType.msoControlSplitButtonPopup, Microsoft.Office.Core.MsoControlType.msoControlSplitButtonMRUPopup
- Dim sctl As Office.CommandBarControl, ctlPopup As Office.CommandBarPopup
- ctlPopup = cmm
- nodd.Text = cmm.caption
- For Each sctl In ctlPopup.Controls
- add(cmm, menuitem, nodd)
- Next
- Case Else
- nodd.Text = cmm.caption
- add(cmm, menuitem, nodd)
- End Select
- Next
- Next
- End Sub
- Sub DumpControl1(ByVal ctl As Office.CommandBarControl, ByVal level As TreeNode) '递归写入NODE
- Dim node As New TreeNode
- node.Text = ctl.Caption & ctl.Id
- level.Nodes.Add(node)
- Select Case ctl.Type
- Case Microsoft.Office.Core.MsoControlType.msoControlPopup, Microsoft.Office.Core.MsoControlType.msoControlGraphicPopup, Microsoft.Office.Core.MsoControlType.msoControlButtonPopup, Microsoft.Office.Core.MsoControlType.msoControlSplitButtonPopup, Microsoft.Office.Core.MsoControlType.msoControlSplitButtonMRUPopup
- Dim sctl As Office.CommandBarControl, ctlPopup As Office.CommandBarPopup
- ctlPopup = ctl
- For Each sctl In ctlPopup.Controls
- DumpControl1(sctl, level)
- Next
- Case Else
- '无子菜单
- End Select
- End Sub
- Sub DumpBars1() '子菜单写入node
- Dim xlapp As Excel.Application = CType(Globals.ThisAddIn.Application, Excel.Application)
- Dim l As New TreeNode
- l.Text = xlapp.CommandBars(0).Controls(0).Caption
- Me.TreeView1.Nodes.Add(l)
- DumpControl1(xlapp.CommandBars(0).Controls(0), l)
- Dim bar As Office.CommandBar
- For Each bar In xlapp.Application.CommandBars
- DumpControl1(bar, l)
- Next
- End Sub
- End Class
复制代码
[ 本帖最后由 wqfzqgk 于 2011-6-27 07:58 编辑 ] |
|