Jamie Garroch (MVP)
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:
And here is a simple VBA callback procedure to determine which button was pressed:
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?
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>
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
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?
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.
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"
CommandBars.ExecuteMso "PasteSourceFormatting"
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.
ASKER
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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:
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"
This generates the callback which I can see in debug mode in VBA.
ASKER
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:
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
While this returns 1:?CommandBars.FindControls(, Id:=571).Count
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>
You may be using this already, but there is an improved CustomUI editor here: office-ribbonx-editor
ASKER
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.