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.
Related
Good morning, I need my VBA code to run right before the TaskItem is saved, is there any way I can handle the event of saving ? I cannot find anything I could use in documentation. I am using MS-Office 2010.
Edit: I have tried
Private Sub TaskItem_Quit()
The macro disappears from macro list, but the code still does not run.
Try the Write event.
https://msdn.microsoft.com/en-us/library/office/ff868664.aspx
"Occurs when an instance of the parent object is saved, either explicitly (for example, using the Save or SaveAs methods) or implicitly (for example, in response to a prompt when closing the item's inspector)."
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.
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.
I am trying to have some code fire when the document is first loaded, but there are two problems. First, I don't know which method to call to get something to fire when the document is first opened. Second, if they have macros disabled, how can I be sure that it gets called when they are enabled?
Thanks!
The Document_Open event is sent when your document is first loaded. To make use of it, enter the following in the VBA code for ThisDocument:
Private Sub Document_Open()
'// your code goes here'
End Sub
As for the disabled macros, I'm not aware of a method that will be called as soon as macros are enabled.
I am executing code on the WindowSelectionChange event in Microsoft Word. How come when I open another document, that does not have this macro referenced in it, the code is still being called on WindowSelectionChange. I do not have the macro stored in the default template, it is stored in a template not referenced by my other word documents.
Is there a way to limit this event to the document that has the VBA code in it?
The macro events only fire unwantedly if the document that they are supposed to fire in is open at the same time. The project is stored in my own .dot file, not the default template.
Is this just the nature of binding events in word? It effects all open documents. Any ideas?
I believe the WindowSelectionChange event is called at the application level, right? So basically I think the way to fix this is to modify the code in the WindowSelectionChange so that it only executes if ActiveDocument = ThisDocument. ThisDocument is the document where your code is stored, so you can make it so the code executes only when ThisDocument is the active document in Word.
Is it possible that you made the macro in the default template instead of in the specific document?
Yes, in the macro editor, make sure your code is in the project for your document and the the one called "Normal".