Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel:XML-ContextMenuList的ContextMenu编辑_Xml_Vba_Excel_Contextmenu - Fatal编程技术网

Excel:XML-ContextMenuList的ContextMenu编辑

Excel:XML-ContextMenuList的ContextMenu编辑,xml,vba,excel,contextmenu,Xml,Vba,Excel,Contextmenu,我试图隐藏Excel的元素。我的问题是我在任何地方都找不到孩子idMso。(甚至不在Office UI帮助文件中) 我一直在猜测,这对我来说毫无意义。有人知道我在哪里可以找到这些信息吗?最有用的是查看驱动此上下文菜单的XML 到目前为止,我的猜测是: <contextMenus> <contextMenu idMso="ContextMenuListRange"> <button visible = "false" idMso = "Cut"/>

我试图隐藏Excel的
元素。我的问题是我在任何地方都找不到孩子
idMso
。(甚至不在Office UI帮助文件中)

我一直在猜测,这对我来说毫无意义。有人知道我在哪里可以找到这些信息吗?最有用的是查看驱动此上下文菜单的
XML

到目前为止,我的猜测是:

<contextMenus>
   <contextMenu idMso="ContextMenuListRange">
    <button visible = "false" idMso = "Cut"/>
    <button visible = "false" idMso = "Copy"/>
    <button visible = "false" idMso = "InsertComment"/> <!--Not correct-->
    <button visible = "false" idMso = "FormatCells"/> <!--Not correct-->
    <button visible = "false" idMso = "Hyperlink"/> <!--Not correct-->
   </contextMenu>
</contextMenus>

对于此类任务,您应该使用


您可以阅读“Excel 2016 Power Programming with VBA”一书中的章节,以获取更多信息。

您可以使用类似于以下内容的方法来插入注释控件:

Application.CommandBars("Cell").FindControl(ID:=2031).Enabled = False

Alistair Mcmillan在post上有代码,可以为您提供命令所需的ID的完整列表。我正在链接到它,但没有重新发布,因为它不是我可以发布的。

这里有一些VBA代码,可以在控件列表中迭代并输出它们的名称(菜单文本)和ID。从这里,您可以遍历控件并按名称或ID启用/禁用

' Delete the custom controls with the Tag : My_Cell_Control_Tag.
    For Each ctrl In ContextMenu.Controls
        If ctrl.Tag = "My_Cell_Control_Tag" Then
            ctrl.Delete
        End If
    Next ctrl
要列出每个控件的完整代码,可以在循环中执行某些操作来删除每个控件,但要保留的控件除外

Sub ControlList()
  Dim ContextMenu As CommandBar
  Dim ctrl As CommandBarControl

  ' Set ContextMenu to the Cell context menu.
  Set ContextMenu = Application.CommandBars("Cell")

  Set xmlControl = CreateObject("MSXML2.DOMDocument")
  xmlControl.LoadXML ("<xml/>")



  For Each ctrl In ContextMenu.Controls
    Set ndChild = xmlControl.createNode(1, "control", "")
    ' Call ndChild.setAttribute("tooltip", ctrl.TooltipText)
    Call ndChild.setAttribute("caption", ctrl.Caption)
    Call ndChild.setAttribute("visible", ctrl.Visible)
    Call ndChild.setAttribute("id", ctrl.ID)
    Set ndChild = xmlControl.DocumentElement.appendChild(ndChild)
  Next ctrl
  ' xmlControl.Save ("c:\temp\controllist.xml")
  MsgBox xmlControl.xml
End Sub

<xml>
  <control caption="Cu&amp;t" visible="-1" id="21" />
  <control caption="&amp;Copy" visible="-1" id="19" />
  <control caption="&amp;Paste" visible="-1" id="22" />
  <control caption="Paste &amp;Special..." visible="-1" id="21437" />
  <control caption="&amp;Paste Table" visible="-1" id="3624" />
  <control caption="&amp;Insert..." visible="-1" id="3181" />
  <control caption="&amp;Delete..." visible="-1" id="292" />
  <control caption="Clear Co&amp;ntents" visible="-1" id="3125" />
  <control caption="Sp&amp;arklines" visible="0" id="31623" />
  <control caption="Filt&amp;er" visible="-1" id="31402" />
  <control caption="S&amp;ort" visible="-1" id="31435" />
  <control caption="Insert Co&amp;mment" visible="-1" id="2031" />
  <control caption="Delete Co&amp;mment" visible="0" id="1592" />
  <control caption="Sh&amp;ow/Hide Comments" visible="0" id="1593" />
  <control caption="&amp;Format Cells..." visible="-1" id="855" />
  <control caption="Pic&amp;k From Drop-down List..." visible="-1" id="1966" />
  <control caption="&amp;Show Phonetic Field" visible="0" id="1614" />
  <control caption="Define N&amp;ame..." visible="-1" id="13380" />
  <control caption="&amp;Hyperlink..." visible="-1" id="1576" />
  <control caption="Edit &amp;Hyperlink..." visible="0" id="1577" />
  <control caption="&amp;Open Hyperlink" visible="0" id="1015" />
  <control caption="&amp;Remove Hyperlink" visible="0" id="3626" />
  <control caption="E&amp;xpand to detail" visible="0" id="11299" />
  <control caption="Additional Act&amp;ions" visible="0" id="31595" />
  <control caption="F&amp;ull Screen" visible="0" id="178" />
  <control caption="&amp;Additional Actions" visible="-1" id="22577" />
</xml>
Sub-ControlList()
将上下文菜单设置为命令栏
将ctrl键调整为CommandBarControl键
'将ContextMenu设置为单元格上下文菜单。
Set ContextMenu=Application.commandbar(“单元格”)
设置xmlControl=CreateObject(“MSXML2.DOMDocument”)
xmlControl.LoadXML(“”)
对于ContextMenu.Controls中的每个ctrl键
Set ndChild=xmlControl.createNode(1,“control”,“”)
'调用ndChild.setAttribute(“工具提示”,ctrl.TooltipText)
调用ndChild.setAttribute(“caption”,ctrl.caption)
调用ndChild.setAttribute(“可见”,ctrl.visible)
调用ndChild.setAttribute(“id”,ctrl.id)
Set ndChild=xmlControl.DocumentElement.appendChild(ndChild)
下一个ctrl键
'xmlControl.Save(“c:\temp\controllist.xml”)
MsgBox xmlControl.xml
端接头

乍一看,我以为我很快就会有一个idMSO的列表,但结果并不是那么简单

文档仅限于手动RibbonX设计,因为Microsoft推荐的(和)方法是Visual Studio 2017(针对个人开发人员)

除此之外,你最好还是坚持使用VBA

如果您没有尝试过,还有:

Office 2010的上下文菜单加载项
来自Microsoft的com加载项将
idMso
name添加到每个上下文菜单的底部。一种确定在RibbonX中使用的
idMso
的简单方法。(需要
.NET Framework
3.5版)

  • 从Ron de Bruin的网站下载Microsoft ContextMenusaddin

  • 更多信息,包括可下载的动态菜单示例文件

我无法让它工作,因为它是特定于2010年,但它听起来很有希望

除此之外,你最好还是坚持使用VBA

我拼凑了一个Excel函数来列出174个命令栏中的所有1600个控件,但它也可以很容易地修改工具栏

例如,此语句禁用上下文菜单上的
插入注释
命令:(和
=TRUE
以重新启用)

…以及
获取属性值的示例:

Debug.Print Application.CommandBars("Cell").FindControl(ID:=2031).Caption
…或者,相同的结果,但使用CallByName以便属性名称可以来自变量(或直接解析):

Commandbar控件本身没有
名称
(它们有
标题
accDescription
工具脚本文本
accName
等,但没有像
Commandbar那样的常规
名称
)因此,引用它们的最佳方法是使用
ID


样本输出: 您提到的错误是因为每个
命令栏
不一定都有每个
属性
——但是
下一步恢复
会解决这个问题。由于每个版本的Excel都有轻微的
CommandBar
差异,因此最好在您的计算机上生成列表,以说明我们版本之间的差异

Option Explicit

Sub listContextMenuCommands()

    Const wkshtName = "Commands2"
    Const startRow = 1
    Dim commandbarIndex As Integer
    Dim cIndex As Integer, sht As Worksheet, cb As CommandBar
    Dim cellCmd As Variant, testVar As Variant
    Dim col As Integer, rw As Integer

    'list of selected properties to report upon
    cellCmd = Array("accChild", "accChildCount", "accDefaultAction", "accDescription", "accDoDefaultAction", _
        "accFocus", "accHelp", "accHelpTopic", "accHitTest", "accKeyboardShortcut", "accLocation", "accName", _
        "accNavigate", "accParent", "accRole", "accSelect", "accSelection", "accState", "accValue", "AdaptiveMenu", _
        "AddRef", "BeginGroup", "BuiltIn", "Caption", "Context", "Controls", "Creator", "DescriptionText", _
        "Enabled", "GetIDsOfNames", "GetTypeInfo", "GetTypeInfoCount", "Height", "HelpContextID", "Id", "Index", _
        "InstanceId", "InstanceIdPtr)", "Invoke", "IsPriorityDropped", "Left", "Name", "NameLocal", "OLEUsage", _
        "OnAction", "Parameter", "Parent", "Position", "Priority", "Protection", "QueryInterface", "Release", _
        "RowIndex", "Tag", "TooltipText", "Top", "Type", "Visible", "Width")

    'prepare worksheet for output
    Set sht = Sheets(wkshtName)
    rw = startRow
    If MsgBox("Existing data will be cleared from worksheet '" & wkshtName & "'.", vbOKCancel, "Erase data?") <> vbOK Then Exit Sub
    sht.Cells.ClearContents 'delete all values
    sht.Activate

    'populate headings
    sht.Cells(rw, 1) = "CommandBar"
    sht.Range(Cells(rw, 2), Cells(rw, UBound(cellCmd) + 1)) = cellCmd 'dump array of headings into range
    On Error Resume Next 'errors will be generated for properties unavailable for certain commands

    For commandbarIndex = 1 To Application.CommandBars.Count 'enumerate all command bars

        Set cb = Application.CommandBars(commandbarIndex) ' refer to commandbar by name (like "Cell"=context menu) or by Index #
        For cIndex = 1 To cb.Controls.Count ' enumerate all controls on commandbar
            testVar = CallByName(cb.Controls(cIndex), "Index", VbGet)
            If Err Then 'error: control doesn't exist
                Debug.Print "No cIndex : Commandbar: #" & cb.Index & " '" & cb.Name & "' Ctl# " & cIndex
                Err.Clear 'clear error
                GoTo nextCtl 'skip this command
            End If

            rw = rw + 1 'next row
            sht.Cells(rw, 1) = cb.Name 'title of command bar in first column

            For col = 1 To UBound(cellCmd) 'populate each [col] for this [rw]
                'use "CallByName" so we can specify control name with variables (array)
                sht.Cells(rw, col + 1) = CallByName(cb.Controls(cIndex), cellCmd(col - 1), VbGet)
                If Err Then 'catch error
                    sht.Cells(rw, col + 1) = "{N/A}" 'property not available for this control
                    Err.Clear 'clear error
                End If

            Next col 'next column
            Application.StatusBar = "Listing Commands: " & Format(commandbarIndex / Application.CommandBars.Count, "0.0%")

nextCtl:
        Next cIndex  'next control
        DoEvents
    Next commandbarIndex 'next commandbar

    sht.[a1].Select
    sht.Columns.AutoFit

    Debug.Print "Finished " & Application.CommandBars.Count & " Command Bars, " & rw - startRow & " commands."
End Sub
选项显式
子列表ContextMenuCommand()
Const wkshtName=“Commands2”
常数startRow=1
Dim commandbarIndex为整数
Dim cIndex为整数,sht为工作表,cb为命令栏
Dim cellCmd作为变量,testVar作为变量
Dim col为整数,rw为整数
'要报告的选定属性的列表
cellCmd=Array(“accChild”、“accChildCount”、“accDefaultAction”、“accDescription”、“accDoDefaultAction”_
“accFocus”、“accHelp”、“accHelpTopic”、“accHitTest”、“accKeyboardShortcut”、“accLocation”、“accName”_
“accNavigate”、“accParent”、“accRole”、“accSelect”、“accSelection”、“accState”、“accValue”、“AdaptiveMenu”_
“AddRef”、“BeginGroup”、“内置”、“标题”、“上下文”、“控件”、“创建者”、“DescriptionText”_
“已启用”、“GetIDsOfNames”、“GetTypeInfo”、“GetTypeInfoCount”、“高度”、“HelpContextID”、“Id”、“索引”_
“InstanceId”、“InstanceIdPtr”、“Invoke”、“IsPriorityDropped”、“Left”、“Name”、“NameLocal”、“OLEUsage”_
“OnAction”、“Parameter”、“Parent”、“Position”、“Priority”、“Protection”、“QueryInterface”、“Release”_
“行索引”、“标记”、“工具文字”、“顶部”、“类型”、“可见”、“宽度”)
'为输出准备工作表
Set sht=图纸(wkshtName)
rw=startRow
如果MsgBox(“将从工作表中清除现有数据”&“wkshtName&”.”,vbOKCancel,“擦除数据?”)vbOK,则退出Sub
sht.Cells.ClearContents'删除所有值
短促激活
'填充标题
短单元格(rw,1)=“命令栏”
短程范围(单元格(rw,2),单元格(rw,UBound(cellCmd)+1)
Debug.Print CallByName(Application.CommandBars("Cell").Controls(cIndex), "Caption", VbGet)
   Index      Id   accState   Caption                       Enabled
       1      21    1048576   Cu&t                           TRUE
       2      19    1048576   &Copy                          TRUE
       3      22          1   &Paste                         FALSE
       4   21437          1   Paste &Special...              FALSE
       5    3624          1   &Paste Table                   FALSE
       6   25536    1048576   Smart &Lookup                  TRUE
       7   32736      32768   Tran&slate                     TRUE
       8   32713      32768   &Linked Entity                 TRUE
       9    3181    1048576   &Insert...                     TRUE
      10    3181      32768   &Insert...                     TRUE
      11     292    1048576   &Delete...                     TRUE
      12    3125    1048576   Clear Co&ntents                TRUE
      13   24508    1048576   &Quick Analysis                TRUE
      14   31623      32768   Sp&arklines                    TRUE
      15   31402    1048576   Filt&er                        TRUE
      16   31435    1048576   S&ort                          TRUE
      17    2031    1048576   Insert Co&mment                TRUE
      18    1592      32769   Delete Co&mment                FALSE
      19    1593      32769   Sh&ow/Hide Comments            FALSE
      20     855    1048576   &Format Cells...               TRUE
      21    1966    1048576   Pic&k From Drop-down List...   TRUE
      22    1614      32768   &Show Phonetic Field           TRUE
      23   13380    1048576   Define N&ame...                TRUE
      24    1576      32768   &Hyperlink...                  TRUE
      25    1577      32769   Edit &Hyperlink...             FALSE
      26    1015      32769   &Open Hyperlink                FALSE
      27    3626      32769   &Remove Hyperlink              FALSE
Option Explicit

Sub listContextMenuCommands()

    Const wkshtName = "Commands2"
    Const startRow = 1
    Dim commandbarIndex As Integer
    Dim cIndex As Integer, sht As Worksheet, cb As CommandBar
    Dim cellCmd As Variant, testVar As Variant
    Dim col As Integer, rw As Integer

    'list of selected properties to report upon
    cellCmd = Array("accChild", "accChildCount", "accDefaultAction", "accDescription", "accDoDefaultAction", _
        "accFocus", "accHelp", "accHelpTopic", "accHitTest", "accKeyboardShortcut", "accLocation", "accName", _
        "accNavigate", "accParent", "accRole", "accSelect", "accSelection", "accState", "accValue", "AdaptiveMenu", _
        "AddRef", "BeginGroup", "BuiltIn", "Caption", "Context", "Controls", "Creator", "DescriptionText", _
        "Enabled", "GetIDsOfNames", "GetTypeInfo", "GetTypeInfoCount", "Height", "HelpContextID", "Id", "Index", _
        "InstanceId", "InstanceIdPtr)", "Invoke", "IsPriorityDropped", "Left", "Name", "NameLocal", "OLEUsage", _
        "OnAction", "Parameter", "Parent", "Position", "Priority", "Protection", "QueryInterface", "Release", _
        "RowIndex", "Tag", "TooltipText", "Top", "Type", "Visible", "Width")

    'prepare worksheet for output
    Set sht = Sheets(wkshtName)
    rw = startRow
    If MsgBox("Existing data will be cleared from worksheet '" & wkshtName & "'.", vbOKCancel, "Erase data?") <> vbOK Then Exit Sub
    sht.Cells.ClearContents 'delete all values
    sht.Activate

    'populate headings
    sht.Cells(rw, 1) = "CommandBar"
    sht.Range(Cells(rw, 2), Cells(rw, UBound(cellCmd) + 1)) = cellCmd 'dump array of headings into range
    On Error Resume Next 'errors will be generated for properties unavailable for certain commands

    For commandbarIndex = 1 To Application.CommandBars.Count 'enumerate all command bars

        Set cb = Application.CommandBars(commandbarIndex) ' refer to commandbar by name (like "Cell"=context menu) or by Index #
        For cIndex = 1 To cb.Controls.Count ' enumerate all controls on commandbar
            testVar = CallByName(cb.Controls(cIndex), "Index", VbGet)
            If Err Then 'error: control doesn't exist
                Debug.Print "No cIndex : Commandbar: #" & cb.Index & " '" & cb.Name & "' Ctl# " & cIndex
                Err.Clear 'clear error
                GoTo nextCtl 'skip this command
            End If

            rw = rw + 1 'next row
            sht.Cells(rw, 1) = cb.Name 'title of command bar in first column

            For col = 1 To UBound(cellCmd) 'populate each [col] for this [rw]
                'use "CallByName" so we can specify control name with variables (array)
                sht.Cells(rw, col + 1) = CallByName(cb.Controls(cIndex), cellCmd(col - 1), VbGet)
                If Err Then 'catch error
                    sht.Cells(rw, col + 1) = "{N/A}" 'property not available for this control
                    Err.Clear 'clear error
                End If

            Next col 'next column
            Application.StatusBar = "Listing Commands: " & Format(commandbarIndex / Application.CommandBars.Count, "0.0%")

nextCtl:
        Next cIndex  'next control
        DoEvents
    Next commandbarIndex 'next commandbar

    sht.[a1].Select
    sht.Columns.AutoFit

    Debug.Print "Finished " & Application.CommandBars.Count & " Command Bars, " & rw - startRow & " commands."
End Sub