Link to home
Create AccountLog in
Avatar of Jamie Garroch (MVP)
Jamie Garroch (MVP)Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Using VBA to repurpose built-in Microsoft PowerPoint controls for Paste, paste With Source Formatting and paste Using Destination Theme

The Office fluent UI supports repurposing of button, toggleButton and checkBox controls.

I want to repuropse the following three paste commands in PowerPoint which are all defined as button controls in the Microsoft documentation:

Paste, PasteSourceFormatting, PasteDestinationTheme

Here is the resulting customUI:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
	<commands>
		<!-- Repurpose built in button, toggleButton or checkBox controls -->
		<command idMso="Paste" onAction="OnAction"/>
		<command idMso="PasteSourceFormatting" onAction="OnAction"/>
		<command idMso="PasteDestinationTheme" onAction="OnAction"/>
	</commands>
</customUI>

Open in new window


And here is a simple VBA callback procedure to determine which button was pressed:

Sub OnAction(control As IRibbonControl, ByRef cancelDefault)
    Debug.Print control.Id
    cancelDefault = False
End Sub

Open in new window


If I now copy a slide in PowerPoint and paste it using Ctrl+V I see the debug output for "Paste".

If I then use the Paste drop down menu in the Home tab to use either of the other two commands I don't see any debug output nor error so it seems these controls are not being repurposed.

This digitally signed project file includes the above example:

repurposing-paste-commands.pptm

Why isn't this working?
Avatar of John Korchok
John Korchok
Flag of United States of America image

I think it's rebelling against a macro named "OnAction". I changed the macro to "MyMacro", changed the corresponding XML, and it started working here.
Avatar of Jamie Garroch (MVP)

ASKER

What?! That's super weird John. My original source code had a different callback name and I simplified it to the Microsoft default name for posting the question here. I changed it to what you have and I'm still only getting the Paste idMso coming through. Furthermore, if I issue the following commands in the Immediate pane with a slide on the clipboard I do see the callback getting fired:
CommandBars.ExecuteMso "PasteDestinationTheme"

Open in new window

CommandBars.ExecuteMso "PasteSourceFormatting"

Open in new window

Which is making me question what idMso the PowerPoint UI is actually using. But that doesn't explain why it's working for you. Do you get all three idMso controls reported? I'm using Version 2004 Build 12718.20010 Click-to-Run, Office Insider.
And, when issuing those commands in the Immediate pane, I'm not presented with the mini paste menu next to the slide thumbnail as it's pasted into the deck whereas I am when pasting via the PowerPoint UI. Something odd going on.
ASKER CERTIFIED SOLUTION
Avatar of John Korchok
John Korchok
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I think you might be on to something there. I added PasteApplyStyle (Ctrl+Shift+V) and PasteDuplicate (Ctrl+D) button control IDs to the project. PasteApplyStyle only appears once in the documented controls list and as "Not in the ribbon". PasteDuplicate appears three times, oddly both in and not in the ribbon!

I can repurpose PasteDuplicate but not PasteApplyStyle.

This doesn't yet explain why repurposing does work when I call one of these failing controls via the Immediate window like this:

CommandBars.ExecuteMso "PasteApplyStyle"

Open in new window

This generates the callback which I can see in debug mode in VBA.
Hmmm. Maybe this also helps determine what's going on:
PasteSourceFormatting = Id 18060
PasteDuplicate = Id 571
The first can't be found in the CommandBars collection even if it can be executed and this returns an error:
?CommandBars.FindControls(, Id:=18060).Count

Open in new window

While this returns 1:
?CommandBars.FindControls(, Id:=571).Count

Open in new window

Probably not useful, but I added PasteSourceFormatting and PasteDestinationTheme to the Ribbon and they started firing the macro:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
	<commands>
		<!-- Repurpose built in button, toggleButton or checkBox controls -->
		<command idMso="PasteSourceFormatting" onAction="MyMacro"/>
		<command idMso="PasteDestinationTheme" onAction="MyMacro"/>
	</commands>
    <ribbon startFromScratch="false">
        <tabs>
            <tab
                id="TabTest"
                insertBeforeMso="TabDesign"
                label="Test">
                <group
                    id="GroupTest">
                    <button
                        idMso="PasteSourceFormatting"
                        showImage="true"
                        showLabel="false"/>
                     <button
                        idMso="PasteDestinationTheme"
                        showImage="true"
                        showLabel="false"/>
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

Open in new window

You may be using this already, but there is an improved CustomUI editor here: office-ribbonx-editor
Interesting. I think this reflects the CommandBars.ExecuteMso behaviour I was seeing. So this means that when these two commands are either inside the PasteGallery (Ribbon/Home/Clipboard/Paste) or PasteGalleryMini control (context menu - note it's type is control not gallery) then they are not repurposable but when called from anywhere else e.g. Ribbon, QAT (I just checked) and CommandBars.ExecuteMso they are repurposable.