PowerPoint VBA - Preserving Custom CommandBar Settings on Close - vba

I have developed a PowerPoint add-in for versions 2003 and earlier that generates a custom commandbar/toolbar on install.
I have no difficulty removing this commandbar on uninstall, but because it uses the Auto_Close event to do so, it also deletes the toolbar every time PowerPoint closes, preventing the user from permanently customizing the commandbar's position.
I've tried a conditional delete by checking if the add-in is registered or loaded, but Auto_Close seems to run before any unloading or deregistration.
Any ideas on how to delete the commandbar ONLY when the add-in is being uninstalled?
Sub Auto_Close()
Dim pptAddin As AddIn
For Each pptAddin In AddIns
If pptAddin.Name = "AddInName" And _
pptAddin.Registered <> msoTrue Then
Application.CommandBars("CommandBarName").Delete
End If
Next
End Sub

It's a good idea to do what you're doing - deleting your CommandBar when PowerPoint closes. That way if there is ever an error, your add-in doesn't leave artifacts in the UI that don't work.
For managing state though, many folks use GetSetting and SaveSetting for reading/writing to a sandboxed area in the Registry for VB/VBA programs. Look up the following functions on this page: GetSetting, SaveSetting, GetAllSettings, DeleteSetting. You can use these to manage your add-in's CommandBar between PowerPoint instances. It's relatively simple to use - here's a tutorial for Excel that would apply equally to PowerPoint.

Related

Purge an IMAP folder in Outlook 2016 using VBA

In Outlook 2016 I have connected an IMAP folder. Outlook is configured to mark items as deleted in IMAP folders. This is necessary because I want to process those items, that are marked for deletion seperately. After my macro is completed, that IMAP folder should be purged automatically.
In Outlook there is the ribbon "Folder" with the group "purge". In there is the menu "delete" with an menu item to purge the current folder. I can't find a way to execute the function from a VBA macro.
For Outlook 2010 there is this solution:
http://www.vboffice.net/en/developers/purge-deleted-imap-messages/
In Outlook 2016, the findControl method does not find the required control.
Is there any way to purge that folder?
Best solution would be some kind of API function.
Second best would be to simulate a click event on the control of the ribbon. "CommandBars" seems to contain some sort of context menus but not the ribbon controls.
Is it possible to define custom commandbars with standard controls in it? The control id seams to be still "12771".
I very briefly looked into the UI Automation toolkit but have found no good example of how to access the ribbon of a specific application.
Alternatively: can I get access to controls of the quick access toolbar? Adding the correct purge folder control to the quick access toolbar would rely on the user to click on that button at the right moment.
For buttons you can add to a ribbon or the QAT, the idMso can be seen at the end of the text when hovering over the command.
Sub PurgeFolder_Button_idMso()
' For buttons you can add to a ribbon or the QAT,
' the idMso can be seen at the end of the text when hovering over the command.
ActiveExplorer.CommandBars.ExecuteMso ("PurgeFolder")
End Sub

Cannot edit Excel Menus using VBA

I'm trying to create my own menu in Excel's (2010) Ribbon.
I wrote the following Sub to create a 'test' menu but I cannot find any evidence that the menu was created other than it being listed in the Controls Collection.
I feel like I've done everything correctly especially since I looked up bunch of code samples (example, this does not work for me either) from other people as well.
I guess my question is twofold, does the code below produce a new menu in YOUR Excel, and if so any idea why would this fail on my computer?
Sub CreateInterface()
Dim Controls As CommandBarControls
Dim CmdBar As CommandBarControl
Dim NewMenu As CommandBarControl
Set Controls = Application.CommandBars("Worksheet Menu Bar").Controls
Set NewMenu = Controls.Add(Type:=msoControlPopup, Temporary:=True, before:=Controls.Count)
NewMenu.Caption = "&Test"
NewMenu.Visible = True
For Each CmdBar In Controls
Debug.Print CmdBar.Caption & "|" & CmdBar.Tag
Next
End Sub
EDIT:
Thanks to D.O. I can see that the code is working it just isn't creating a new menu in the ribbon (like the default ones (Home, Insert, Page Layout, etc.)).
That being said to the best of my knowledge this was supposed to create a new menu, not an item in one of the menus, how can I actually create a new menu?
To the best of my knowledge, the old (pre-2007) CommandBars and Controls way is obsolete enough that it only adds buttons to the "Add-ins" ribbon tab. This is for backward compatibility with old Office solutions, so that they can still be used (somewhat) under 2007+ Office apps.
The way forward (if you plan on using Office 2007 and up) is to modify the ribbon itself. It's a completely new paradigm, and real-time customizations are done in a completely different way, but if you get the hang of it (it's not that difficult) it is quite useful.I suggest Googling some ribbon tutorials, and using the "Custom UI editor" tool to add your ribbon code to your Office files. Ron de Bruin's web site is always a good resource for more information.

How to Differentiate SaveAs call and Save call in PowerPoint events?

I'm writting AddIn for PowerPoint 2010. I'm using two functions of PowerPoint.
Application_PresentationBeforeSave(ByVal Pres As Microsoft.Office.Interop.PowerPoint.Presentation, ByRef Cancel As Boolean)
Application_PresentationSave(ByVal Pres As Microsoft.Office.Interop.PowerPoint.Presentation)
When I perform Save operation (Ctrl+S) or SaveAs (File -> SaveAs) on powerpoint it executes Application_PresentationBeforeSave() method.
But I need to differentiate these two calls (Ctril+S & SaveAs) and accordingly perform some task. So how can I differentiate these two calls in BeforeSave method ??
As for Word, in Application_DocumentBeforeSave(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean) there is SaveAsUI flag which differentiate whether this method has been called by SaveAs or Ctrl+S action.
So is there any flag/property which differtiate same things in PowerPoint ??
You need to repurpose the ribbon buttons or replace the backstage UI controls with your own so you will know what action users chose in the UI. In case of ribbon controls see the Temporarily Repurpose Commands on the Office Fluent Ribbon article in MSDN. The Backstage UI is described in the following articles in MSDN in depth:
Introduction to the Office 2010 Backstage View for Developers
Customizing the Office 2010 Backstage View for Developers
In case of Ctrl+S shortcuts you need set a keyboard hook using Windows API functions, see Using shortcut keys to call a function in an Office Add-in for more information.
Thanks Eugene for showing me a way.
My problem got resolve. I tried your suggestion.
Here is the description of my solution.
I have added function call in Ribbon.xml
In MySaveAs() function I set one glbal variable. And used it in BeforeSave mthod.

Excel Addins - Not Modifying Toolbar

I am looking for the best way to deploy Excel Macros to users. My goal is to make it super easy for end users to install and promote use by adding to the addin toolbar. I know that there are a number of help articles on this topic but couldn't find anything that covered this exact issue. Can you please help and excuse me if this is a noobie question. Please see below for replication steps for my issue.
I have added the code below as a worksheet event on "This Worksheet" of an excel macro file
I add the main code to a module that it references
I save this as an .XLAM in the addin roaming folder
I enable this as an addin in EXCEL 2013
After I install it adds the button to an add in tab
It works until I close Excel in which case the button disappears
It is still under active add ins but not on the toolbar
The code:
Option Explicit
Dim cControl As CommandBarButton
Private Sub Workbook_AddinInstall()
On Error Resume Next 'Just in case
'Delete any existing menu item that may have been left.
Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete
'Add the new menu item and Set a CommandBarButton Variable to it
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
'Work with the Variable
With cControl
.Caption = "Convert Survey Reporter Tables"
.Style = msoButtonCaption
.OnAction = "CMB_General_Table_Formatting"
'Macro stored in a Standard Module
End With
On Error GoTo 0
End Sub
Private Sub Workbook_AddinUninstall()
On Error Resume Next 'In case it has already gone.
Application.CommandBars("Worksheet Menu Bar").Controls("Convert Survey Reporter Tables").Delete
On Error GoTo 0
End Sub
If you want an alternative to using VBA to build the interface, I have previously deployed Excel add-ins (XLAM files) using some variety of Ribbon XML. This allows for very fine-grained control of the resulting interface and does not require you to work in VBA to build the interface. For most applications, I have found it is much easier to build the Ribbon components outside of VBA and then wire up the callbacks in VBA.
For the end user, I think this approach also delivers a better looking add-in since the resulting interface has its own Ribbon tab (or you can add to any of the existing ones) instead of being in the Add-ins Ribbon tab.
If you want to pursue this route, I highly recommend using the Ribbon X Visual Designer to build the interface and set callbacks. I have used it to build an add-in that had more than 50+ features accessible by buttons and other Ribbon form controls. It was fairly painless once I got going.

Remove Add-in in Powerpoint with VBA

I have a VBA Add-in in PowerPoint that is updated occassionally. I wrote a code in VBA where every Monday there is a check wether a newer version of the Add-in exists. And if it does, PowerPoint automatically updates the Add-In. So far, I have this accomplished:
Deactivate the old Add-In in PowerPoint
Delete the old Add-in in Microsoft's Add-In folder
Copy the new Add-In to Microsoft's Add-In folder
Activate the new Add-in in PowerPoint
However, I can't seem to find the right command to delete the old Add-in in PowerPoint. The old Add-in is still in PowerPoint's available Add-ins, and PowerPoint also lists it under the inactive Application Add-ins.
Here's part of the Code that first deactivates the Add-in, and then tries to remove it from PowerPoint.
Dim oAddin As AddIn
For Each oAddin In Application.AddIns
If Left(oAddin.name, 16) = "PPT ACO Add-in V" Then
oAddin.Loaded = msoFalse
Kill (oAddin) '<-- these is the line of Code that doesn't execute
End if
Next oAddin
The program would work, but after some time I'd have a bunch of inactive Add-ins laying around in PowerPoint which will be very confusing to users.
Thank you so much for your help guys.
Tommy
very late answer, but since I passed by and I spent myself quite sometime on this issue, instead of
kill (oAddin)
you need
kill oAddin.fullname
although you should also remove the addin from the collection of AddIns and maybe unregister.
Also, in version 2010, you might get an error of permission denied when you try to delete the file. This is caused by ppt not releasing in time the open addin file. In fact, a timer does not solve it. But a second kill will actually work.