How to Differentiate SaveAs call and Save call in PowerPoint events? - vb.net

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.

Related

Word VSTO - Document Changed Event

we are using the Application.DocumentChange event so that when a document is loaded, it can check the name of the document, and then if it is named in a certain way show or hide buttons on the ribbon.
If I use the code below it works really well, it shows or hides the buttons correctly.
But when I run the addin in debug mode from Visual Studio, when the document is closing it gives this message.
System.Runtime.InteropServices.COMException: 'This command is not available because no document is open.'
As you can see I tried to put in an if statement to stop it running the code but it didnt work.
If I just install my addin, Word does not seem to crash or have any problems, maybe I shouldnt worry about it?
Thanks
Private Sub Application_DocumentChange() Handles Application.DocumentChange
If Globals.ThisAddIn.Application.Documents.Count > 0 Then
If Globals.ThisAddIn.Application.ActiveDocument.BuiltInDocumentProperties("Title").Value = "Document Name Here" Then
Globals.Ribbons.VisualfilesTab.AttachEvidence.Visible = True
Else
Globals.Ribbons.VisualfilesTab.GetAuthorisation.Visible = True
End If
End If
End Sub
Consider switching your custom UI to the ribbon XML over the designer-generated custom ribbon UI. You can export your existing custom UI to the ribbon XML markup. See How to: Export a ribbon from the Ribbon Designer to Ribbon XML for more information.
You can customize the Ribbon UI by using callback procedures in COM add-ins. For each of the callbacks that the add-in implements, the responses are cached.
For example, if an add-in writer implements the getVisible callback procedure for a button, the function is called once, the state is loaded, and then if the visibility state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate or IRibbonUI.InvalidateControl method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.
Read more about the the Fluent UI (aka Ribbon UI) in the following articles:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

When Ribbon button is clicked disable multiple buttons

I have a couple of button is an MS access application that need to be enabled or disabled at the same time. When one of them is clicked and opens a form they both need to be set to disabled. When the form is closed they both need to be set to enabled.
I have no problem doing this for the control that is clicked. But I don't know how to reference the one that was not clicked.
Here is the code for my ribbon:
and the funciton I use to handle getEnabled:
How do I disabled or enable both buttons at the same time?
You need to use the IRibbonUI.Invalidate or IRibbonUI.InvalidateControl methods to get your callback invoked for other controls. The invalidate method allows invalidating the cached values for all of the controls of the Ribbon user interface.
For example, if an add-in writer implements the getImage callback procedure for a button, the function is called once, the image loads, and then if the image needs to be updated, the cached image is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.
Dim MyRibbon As IRibbonUI
Sub MyAddInInitialize(Ribbon As IRibbonUI)
Set MyRibbon = Ribbon
End Sub
Sub myFunction()
MyRibbon.Invalidate() ' Invalidates the caches of all of this add-in's controls
End Sub
The Fluent UI (aka Ribbon UI) is described in depth in the following series of articles:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

Disable "save" with office addin VB.NET

I'm making an AppLock for Office and I want to lock features, like open, save and saveas with a password
Can I make something like this?
Public Sub ThisAddIn_OnSave(sender As Object, e As SaveEventArgs) Handles Me.OnSave
If <my_condition> Then
e.Cancel = True
End If
End Sub
It depends on the Office application you want to integrate with. For example, in Word you can handle the Application.DocumentBeforeSave event.
Otherwise, you can intercept Ribbon button commands and execute your custom code that way.
Repurposing ribbon controls is not enought for preventing users from doing such actions.
A keyboard shortcut can be used (for example, Ctrl+S for saving documents). You need to set a keyboard hook for repurposing keyboard shortcuts, see Using shortcut keys to call a function in an Office Add-in for more information.

word 2007 - programmaticaly add a quick access icon for a macro, or add a quick shortcut key for a macro

I am trying to add a macro to the quick access toolbar through vba code.
I am also wondering how to add a macro key shortcut through vba code.
This is all for windows 2007.
For keyboard shortcut, use:
Application.OnKey
To add to the quick access toolbar, first, you need a reference to the ribbon itself. I had a project where I gave my custom ribbon a custom "onload" function, and then captured the ribbon object in that function. Then, use that object to make changes. Sadly, sometimes this object "disappears" in VBA, and there's not much you can do except have a backup of the object reference, and usually one of them is still active.
See this link for an add-in approach:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
I used this ribbon editor for my project.
Here's how I captured the ribbon object with a custom onload function:
'Callback for customUI.onLoad
Sub RibbonLoaded(ribbon As IRibbonUI)
Set myRibbon = ribbon
Set myRibbonBackup = ribbon
End Sub
I would recommend using the ribbon editor tool I gave you as much as possible, rather than trying to edit the ribbon with VBA--it can be quite difficult. I've never used VBA to modify the quick access toolbar.

PowerPoint VBA - Preserving Custom CommandBar Settings on Close

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.