ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1195|回复: 2

[分享] 获取commandbars子对象的名称与类型

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-6-22 22:26 | 显示全部楼层 |阅读模式
最近在学commandbars,但是每次查找很麻烦,就参考书籍写了下面这段代码,这样找起来就方便多了

Sub test()
Dim k As Integer
Dim cbar As CommandBar

Worksheets("工具栏").UsedRange.ClearContents

tt = Array("name", "namelocal", "type", "type", "type", "height", "width", "builtin", "left", "top", "position", "visible")
Range(Cells(1, 1), Cells(1, UBound(tt, 1))).Value = tt
For Each cbar In CommandBars
    With Worksheets("工具栏")
        .Cells(k + 2, 1) = cbar.Name
        .Cells(k + 2, 2) = cbar.NameLocal
        .Cells(k + 2, 3) = IIf(cbar.Type = 1, "菜单栏", IIf(cbar.Type = 0, "默认菜单栏", "快捷菜单"))
        .Cells(k + 2, 4) = IIf(cbar.Type = 1, "msoBarTypeMenuBar", IIf(cbar.Type = 0, "msoBarTypeNormal", "msoBarTypePopup"))
        .Cells(k + 2, 5) = cbar.Type
        .Cells(k + 2, 6) = cbar.Height
        .Cells(k + 2, 7) = cbar.Width
        .Cells(k + 2, 8) = cbar.BuiltIn
        .Cells(k + 2, 9) = cbar.Left
        .Cells(k + 2, 10) = cbar.Top
        .Cells(k + 2, 11) = cbar.Position
        .Cells(k + 2, 12) = cbar.Visible
        .Cells(k + 2, 13) = cbar.Enabled
    End With
    k = k + 1
Next cbar
End Sub


评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-6-23 09:44 | 显示全部楼层
重新改写了下
Sub test()
Dim k As Integer
Dim cbar As CommandBar

Worksheets("工具栏").UsedRange.ClearContents

tt = Array("name", "namelocal", "index", "type", "type", "type", "height", "width", "builtin", "left", "top", "position", "visible", "enable")
Range("a1").Resize(1, UBound(tt) - LBound(tt) + 1).Value = tt
For Each cbar In CommandBars
    With Worksheets("工具栏")
        .Cells(k + 2, 1) = cbar.Name
        .Cells(k + 2, 2) = cbar.NameLocal
        .Cells(k + 2, 3) = cbar.Index
        .Cells(k + 2, 4) = IIf(cbar.Type = 1, "菜单栏", IIf(cbar.Type = 0, "默认菜单栏", "快捷菜单"))
        .Cells(k + 2, 5) = IIf(cbar.Type = 1, "msoBarTypeMenuBar", IIf(cbar.Type = 0, "msoBarTypeNormal", "msoBarTypePopup"))
        .Cells(k + 2, 6) = cbar.Type
        .Cells(k + 2, 7) = cbar.Height
        .Cells(k + 2, 8) = cbar.Width
        .Cells(k + 2, 9) = cbar.BuiltIn
        .Cells(k + 2, 10) = cbar.Left
        .Cells(k + 2, 11) = cbar.Top
        .Cells(k + 2, 12) = cbar.Position
        .Cells(k + 2, 13) = cbar.Visible
        .Cells(k + 2, 14) = cbar.Enabled
    End With
    k = k + 1
Next cbar
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-7-29 21:45 | 显示全部楼层
Sub CommandbarsListing()
Application.ScreenUpdating = False
On Error Resume Next
Dim oCbar As Object
Dim intRowNum As Integer
intRowNum = 1
Dim oSht As Object
Set oSht = ThisWorkbook.Worksheets("工具栏菜单项")
With oSht
    .Cells(intRowNum, 1) = "Index"
    .Cells(intRowNum, 2) = "Name"
    .Cells(intRowNum, 3) = "NameLocal"
    .Cells(intRowNum, 4) = "Type"
    .Cells(intRowNum, 5) = "Position"
    .Cells(intRowNum, 6) = "Proteciton"
    .Cells(intRowNum, 7) = "Top"
    .Cells(intRowNum, 8) = "Left"
    .Cells(intRowNum, 9) = "Width"
    .Cells(intRowNum, 10) = "Height"
   
    .Cells(intRowNum, 11) = "Index"
    .Cells(intRowNum, 12) = "Type"
    .Cells(intRowNum, 13) = "Type"
    .Cells(intRowNum, 14) = "ID"
    .Cells(intRowNum, 15) = "Caption"
    .Cells(intRowNum, 16) = "DescriptionText"
    .Cells(intRowNum, 17) = "OnAction"
    .Cells(intRowNum, 18) = "Parameter"
    .Cells(intRowNum, 19) = "Parent"
    .Cells(intRowNum, 20) = "Tag"
End With
intRowNum = intRowNum + 1
For Each oCbar In Application.CommandBars
    If oCbar.BuiltIn Then
        With oSht
            .Cells(intRowNum, 1) = oCbar.Index
            .Cells(intRowNum, 2) = oCbar.Name
            .Cells(intRowNum, 3) = oCbar.NameLocal
            .Cells(intRowNum, 4) = oCbar.Type
            .Cells(intRowNum, 5) = oCbar.Position
            .Cells(intRowNum, 6) = oCbar.Protection
            .Cells(intRowNum, 7) = oCbar.Top
            .Cells(intRowNum, 8) = oCbar.Left
            .Cells(intRowNum, 9) = oCbar.Width
            .Cells(intRowNum, 10) = oCbar.Height
        End With
        For Each oCtrl In oCbar.Controls
            If oCtrl.BuiltIn Then
                With oSht
                    .Cells(intRowNum, 11) = oCtrl.Index
                    .Cells(intRowNum, 12) = oCtrl.Type
                    .Cells(intRowNum, 13) = strMsoControlType(oCtrl.Type)
                    .Cells(intRowNum, 14) = oCtrl.ID
                    .Cells(intRowNum, 15) = oCtrl.Caption
                    .Cells(intRowNum, 16) = oCtrl.DescriptionText
                    .Cells(intRowNum, 17) = oCtrl.OnAction7
                    .Cells(intRowNum, 18) = oCtrl.Parameter
                    .Cells(intRowNum, 19) = oCtrl.Parent
                    .Cells(intRowNum, 20) = oCtrl.Tag
                End With
            End If
            intRowNum = intRowNum + 1
        Next oCtrl
    End If
Next oCbar

Call ColumnsAutofiting

Application.ScreenUpdating = True
Application.Goto oSht.Range("a1"), True

End Sub

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

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-1-11 03:52 , Processed in 0.017705 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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