Search code examples
vbaribbonpowerpoint

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


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:

enter image description here


Solution

  • 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