Is there a way to disable CommandBar Controls in Powerpoint VBA?

旧街凉风 提交于 2019-12-24 07:18:41

问题


I have a PPT Add-In which may fail if a certain ViewType is not maintained.

I do not see any PPTEvent which I could trap the change and prevent it (although, if this is possible, please advise!). So I have been playing with the Ribbon/CommandBars attempting to disable or hide certain controls pertaining to the ViewType.

I have identified the controls by Id and I attempt to set their .Visible property to False, or alternatively, to .Enabled = False, but neither seems to have any affect. The controls are still visible, and clicking on them still executes.

This example I would try to disable the Slide Sorter control. This prevents VBE from Execute the Button, but it does not disable the button's action as far as the user might still click it, it still executes.

Sub DisableViewChange()

    Dim cBar As CommandBar
    Dim ctrl As CommandBarControl

    Set cBar = CommandBars("View")

    Set ctrl = cBar.FindControl(Id:=738)
        ctrl.Visible = True
        ctrl.Enabled = False
    Set btn = ctrl
        btn.Execute



    Set cBar = Nothing
    Set btn = Nothing
    Set ctrl = Nothing

End Sub

Update to include pics of the elements I would like to disable/hide/remove:


回答1:


You can definitely modify the XML of a specific document to remove certain ribbon elements from the ribbon - I have done a huge amount of this. You should investigate using the Custom UI Editor which will open up the document and allow you to modify the XML of your selected ribbon groups by using the Visible = False modifier. Then, you should be able to save the Add-In and when it's loaded into memory it will hide the parts you've specified, preventing your crash.

This was useful for me a while ago: http://gregmaxey.mvps.org/word_tip_pages/customize_ribbon_main.html

There are Microsoft XML Schemas for each of the Office applications (which I can't find now - but definitely exists).

I hope that's some help.




回答2:


Red's suggestion above was very helpful. With some (errr... a lot) of trial and error, I was able to make some tweaks to the Ribbon. I present this answer in hopes that my trial and error will be useful to someone else who is equally unfamiliar with XML:

I disable several commands that I was looking to disable, that was relatively easy:

<commands>
    <command idMso="ViewSlideSorterView" enabled="false"/>
    <command idMso="ViewNotesPageView" enabled="false"/>
    <command idMso="ViewSlideShowReadingView" enabled="false"/>
    <command idMso="ViewSlideMasterView" enabled="false"/>
    <command idMso="ViewHandoutMasterView" enabled="false"/>
    <command idMso="ViewNotesMasterView" enabled="false"/>
    <command idMso="WindowNew" enabled="false"/>
</commands>

Since I am fussing with the XML, I decide also to migrate my add-in's commands from a legacy CommandBar (under the Add-Ins Tab group) to a custom ribbon tab, so this XML also contains a working example of a new Tab, which consists of a Group, a menu, and a few buttons in that menu.

Here is the validated XML including the disabled commands and the custom tab menu:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <commands>
        <command idMso="ViewSlideSorterView" enabled="false"/>
        <command idMso="ViewNotesPageView" enabled="false"/>
        <command idMso="ViewSlideShowReadingView" enabled="false"/>
        <command idMso="ViewSlideMasterView" enabled="false"/>
        <command idMso="ViewHandoutMasterView" enabled="false"/>
        <command idMso="ViewNotesMasterView" enabled="false"/>
        <command idMso="WindowNew" enabled="false"/>
    </commands>
    <ribbon startFromScratch="false">
        <tabs>
            <tab idMso="TabView">
                <group idMso="GroupPresentationViews" visible="true"/>
                <group idMso="GroupMasterViews" visible="true"/>
            </tab>
            <tab id="MyNewTab" label="My Tab Label">
                <group id="MyGroupMain" label="My Group Label">
                    <menu id="MyMenu" imageMso="HappyFace" size="large">
                        <button id="MyLaunchButton" label="Launch Tiger" onAction="macro1" />
                        <button id="MyInfoButton" label="Info" onAction="macro2" />
                        <button id="MyVersionButton" label="Version" onAction="macro3" />
                        <button id="MyHelpButton" label="Help" onAction="macro4" />
                    </menu>
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

I have not hooked my subroutines/macros in to these buttons yet, but that should not be terribly difficult.

I would not have been able to get through this without stumbling upon this link, which contains a batch of Excel files (for each Application) listing all of the menu items by type, id, relationship to other items, etc.

http://www.microsoft.com/en-us/download/details.aspx?id=6627



来源:https://stackoverflow.com/questions/16860236/is-there-a-way-to-disable-commandbar-controls-in-powerpoint-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!