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.
Related
Is it possible to modify the behavior of existing buttons in outlook (or generally in ms office programs)? E.g. can I make the "send mail" button show a dialog before sending a mail?
I know that you can make add ins and put them into ribbon but can you add certain behavior to existing controls?
Use Microsoft.Office.Interop.Outlook und handle the Send event. You can find the documentation here.
You have use your application reference to get the active inspector and by the active inspector you retrieve the message class:
(CType(inspector.CurrentItem, Outlook.ItemEvents_10_Event)).Send += New Outlook.ItemEvents_10_SendEventHandler(Inspector_Send)
Private Sub Inspector_Send(ByRef Cancel As Boolean)
... your code...
End Sub
Is there a way, how to run macro in MS word after mouse-doubleclick (outside Active-X object)?
something like Document_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Yes, Word has an application-level event for executing VBA code (or VSTO code) when the user double-clicks on a document: WindowBeforeDoubleClick
The event provides as a parameter a Selection object, where the double-click occurred and also provides a Cancel parameter that optionally allows you to suppress the action the double-click would trigger.
Search the event in the MSDN documentation for more information. You'll also find information there about how to work with application-level events, in case you're not familiar with them.
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.
Is there a way to disable the message (and have the document editable by default):
Server Document To modify document, click Edit Document followed by the button with text "Edit Document".
I cannot find a word setting to do this. In addition I cannot see a way to make a VB macro to do this with a key stroke. I have used a small autohotkey script to position the mouse and click this prompt, but this does not always work since it depends on the position of the window. it is impossible to use the tab key to get to this prompt.
I have to modify about 50+ documents a day from sharepoint, ideally I would like to combine this with another macro which does other automated processing for me. But I can't find a VB solution for clicking the Edit button.
Depending on your security settings (you mentioned that they were blocked), this may or may not work.
Create a new macro enabled template in your Word startup folder (usually at C:\Users[YourID]\AppData\Roaming\Microsoft\Word\STARTUP), and add a new class module. I called mine "AutoEditEnable". You can name it anything, but you'll need it to match how you declare it in the other module.
This code goes in the class:
Option Explicit
Private WithEvents app As Application
Private Sub Class_Initialize()
Set app = Application
End Sub
Private Sub app_ProtectedViewWindowOpen(ByVal PvWindow As ProtectedViewWindow)
PvWindow.Edit
End Sub
Basically, this will hook any Application events you need to - in this case the ProtectedViewWindowOpen event or the ProtectedViewWindowActivate event (either should work).
Put the following code in ThisDocument to grab a reference to it when your template loads:
Option Explicit
Private hook As AutoEditEnable
Private Sub Document_Open()
Set hook = New AutoEditEnable
End Sub
Close Word and restart it, then make sure your new template shows up as a loaded add-in.
I have a custom ribbon which works fine but I only want to enable it and have my add in show for certain workbooks so I check for the workbook title on load and try to use the Invalidate method if the condition is false. Unfortunately nothing happens the custom ribbon tab is still showing.
The following is my sub:
Public Sub loadMyRibbon(ribbon As IRibbonUI)
Set RibUI = ribbon
If Not workbookTitle = "My Workbook" Then
If Not RibUI Is Nothing
RibUI.Invalidate
MsgBox "Not Working"
End If
End If
End Sub
Which seems correct to me from reading through the method documentation:
Microsoft Documentation
I see my MsgBox message displayed on the screen so I know the code is executing correctly up to that point but RibUI.Invalidate doesn't hide my tab. Appreciate any pointers!
I have also tried:
RibUI.InvalidateControl "myTag"
But this also doesn't work
Ribbon.Invalidate doesn't mean the ribbon will not show. Invalidate function just tells the ribbon to invalidate and re-initialize the ribbon controls with their default/dynamic properties.
I worked with few Add-ins where the clients wanted to hide the ribbon items if users cannot pass the authentication. So in such a case, I used "GetVisible" attribute in all of my Control and then I used this code
Sub GetVisible(control As IRibbonControl, ByRef Visible)
On Error Resume Next
Visible = shouldShowOrNot
End Sub
shouldShowOrNot is a boolean variable, which I set in Ribbon Load to true if user passes the authentication. See the following image:
Now the above image is a representation of Ribbon in case User has failed the authentication. There might be a better way to do it, but i found it to be the best way so far.
Hope this helps,
Vikas B