word object model - active document event when clicking on FILE - ms-word

I'm developing an MS Word add-in. In newer MS Word editions, there is the "FILE" option in the menu bar which opens an interface where you can select a recent document to open, open a new one, or an existing one. I am trying to find a way, through which I can know WHEN the user "leaves" the current document he is editing clicking on the FILE menu of Word. I cannot seem to find such an event. Is there a way to achieve this ?
The WindowDeactivate does not fulfill this purpose.
The reason I want to do this, is because for a custom spellchecker I'm writing, I'm highlighting the wrong words in an transparent (click through as well) form. So when the user in a recent version of Word clicks the FILE menu, the highlights are still there, as seen in the screenshot
TL:DR; is there a way to detect in MS Word when the user clicks the FILE option in the menu and the current document is not visible? I'm using add-in-express, so all the relevant word object model API is available.
I wonder how can I solve this, any help is appreciated.
edit: screenshot

Yes, you can detect and then execute code both when the File menu is clicked (displaying the Backstage View) and when the View's return arrow is clicked to remove the Backstage View and display the document. To do this use the onShow and onHide attributes with callbacks via a custom XML ribbon in your VSTO project (this will not work with a ribbon made with the Visual Designer).
Reference material can be found here:
Performing Actions When the Backstage View is First Displayed or Hidden
As this article uses VBA to expand on the concepts involved, I built a sample project demonstrating how onShow works using C# and Word 2016 (the documentation was written for Office 2010, but onShow and onHide will work in later versions of Word).
Solution Tree
Custom XML Ribbon (BackstageRibbon.xml)
Note that the <backstage> node, which activates the onShow attribute for the callback, follows the <ribbon> node in the XML.
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"
onLoad="Ribbon_Load">
<ribbon>
<!--Ribbon XML goes here-->
</ribbon>
<backstage onShow="onShow">
</backstage>
</customUI>
Ribbon Code (BackstageRibbon.cs)
A bunch of this code is boilerplate, however public void onShow is the callback that executes your code based on the onShow attribute in the ribbon's custom XML. Also, public string GetCustomUI is where the C# is told to find the XML.
namespace Backstage_Events
{
[ComVisible(true)]
public class BackstageRibbon : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
public BackstageRibbon()
{
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
return GetResourceText("Backstage_Events.BackstageRibbon.xml");
}
#endregion
#region Ribbon Callbacks
//Create callback methods here. For more information about adding callback methods, visit https://go.microsoft.com/fwlink/?LinkID=271226
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
this.ribbon = ribbonUI;
}
public void onShow(object contextObject)
{
//Code to be executed before Backstage View displays goes here
MessageBox.Show("Backstage Display Event Triggered!");
}
#endregion
Helpers //Region
}
}
ThisAddin.cs
You will also need to add:
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new BackstageRibbon();
}
after the ThisAddIn_Startup and ThisAddIn_Shutdown private voids in the ThisAddin.cs class to instantiate the custom ribbon.

Word will fire the Application.DocumentOpen event - you can see it live in OfficeSpy (I am its author - click Application button, go to the Events tab, look at the log at the bottom of the window).

Related

Managing multiple TinyMCE editors on a Vaadin page

I have a page with multiple TinyMCE editors (org.vaadin.tinymce.TinyMce), and I want to know which the user is 'in'. One approach would be to use the focus/blur events, but I've tried adding a class like this:
public class MyTinyMce extends TinyMce implements Focusable<TinyMce> { ... }
and then associating event handlers like this:
myTinyMce.addFocusListener(e -> {
System.out.println("focus!!");
});
myTinyMce.addBlurListener(e -> {
System.out.println("blurred!!");
});
but the events don't fire reliably. If I do the exact same thing with a Text component, it works as expected.
I say not "reliably" because they do fire if I click on the empty space in the TinyMCE toolbar (to the right of 'Tools' in the screenshot), but not if I click on the TinyMCE menu or into the editable body.
screenshot
Am I doing something wrong, or does the TinyMCE component simply not support this use-case? Is there another way to keep track of the user switching between different editors?

GWT: Opening new mail window without browser tab opened

I am trying to open an email client just like using mail me tag.
But I want to use my custom widget, which is not hyperlink, anchor or so. I added a DOM handler to my widget to listen to clicks:
public class VContactWidget extends VHorizontalLayout implements ClickHandler {
private HandlerRegistration clickHandler;
public VContactWidget() {
// added some content here
clickHandler = addDomHandler(this, ClickEvent.getType());
}
#Override
public void onClick(ClickEvent event) {
Window.open("mailto:john.doe#mail.com", "_blank", "");
}
}
Everything is working fine except one detail: When the widget is clicked, new empty browser tab will open with url set to mailto:john.doe#mail.com. I don't want the new tab opened. Can I avoid it somehow?
Note I set _blank parameter, as used in many examples. I also tried to use empty string or some other values as well. I looked into documentation, but didn't find anything useful.
https://developer.mozilla.org/en-US/docs/Web/API/window.open
One solution may be to use Anchor, but my component is more complex, not just single <a> link.
Another detail to note may be application server - I am using Tomcat 7 now.
Trying to fire native event on hidden Anchor programatically did not work for me. (Which does not mean it cannot be done.)
This is, how I actually solved my problem: Instead of Window.open(), I used following call:
public void onClick(ClickEvent event) {
Window.Location.assign("mailto:john.doe#mail.com");
}
This is not something that you can control. Whether this link opens in a new tab or a new window depends on the browser settings and user preferences.
I don't think it will make a difference if you use an Anchor or Window.open. In either case, the behavior may be different in different browsers. Also remember that for some users a click on this link will open Outlook or Mail, while for other users it will open Gmail in a browser window.
UPDATE:
If you want an exact behavior of an <a> element, create a hidden anchor element and fire a click on it when a user clicks on your composite widget.
Firing click event from code in gwt

ASP.NET Button Click event does not appear to fire

I'm relatively new to ASP.NET. I have a ASP.NET MVC 2 Web Application project (created in Visual Studio 2010). I added a method to HomeController called Search
public ActionResult Search()
{
return View();
}
and created a corresponding view (web page) called Search.aspx onto which I dropped a button. I double-clicked the button to add a handler for the button click event which sets the text of a TextBox, then built the application.
<script runat="server">
protected void MyButton1_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Undo button clicked");
m_search_text_box.Text = "MyButton1_Click";
}
...
When I click the button in my browser (I tested in Chrome and Internet Explorer), nothing happens. The text box is not updated. Nothing is written to the Output window either. So, it doesn't look like the event is firing. Can anybody offer any suggestions? I'm using Visual Studio 2010 on Windows 7.
Thanks
You are mixing WebForms event handling into an MVC app. MVC does not work like WebForms. Check out the tutorials on MVC2 to help get you started down the right path.
Here's a sample app with step by step tutorials to help get to the basics of MVC down.
ASP.NET MVC doesn't use code behind handlers like that. You use controller actions to respond to requests, and decide how to visually handle them (ie: you can render a view, or return a JSON object, or redirect to another Action etc).
In your instance, if you want to put some text in a textbox after the user has clicked the button, you'd want to put a Submit button in a form, and create a controller action to respond to it:
[HttpPost]
public ActionResult Search()
{
var model = new SearchModel();
model.StatusText = "MyButton1_Click";
return View(model);
}
In your view, you want to use this model, and put its StatusText property value into a textbox:
<%= Html.TextBoxFor(x => x.StatusText) %>
Have a look at the ASP.NET MVC website which has a lot of great getting started tutorials, and the Nerd Dinner tutorial (a free chapter in the book).

I have a VSTO application as an add-in to MS Word and I want to set keyboard shortcuts to the ribbon buttons

When I developed this app (in C# Visual Studio 2008) I asked the same question (actually managed to find an answer on the MS forum, for which I deserve a prize of some sort). The answer from MS was that the only way to set keyboard shortcuts to your own methods is to write a macro which invokes the method (via COM. obviously) and set the shortcut to invoke that macro.
This is really not the answer I want to hear. VSTO makes it possible to build a really nice application with very good use of the ribbon, etc., but then you have to go to the trouble of exposing the entire thing through COM and build another interface into it via the macros. Which, in addition to being a waste of time, totally circumvents all the security that MS have built into support of VSTO Add-ins.
My question is: Is this really necessary (the whole COM/macro thing), or is there a way that I can assign a keyboard shortcut to my own ribbon items? Word 2007? Word 2010?
Thanks
Its too late to answer but worth sharing.
I have used Keyboard shortcuts in my project. Basically this shortcut key should bring a WPF form called SignOff. This WPF form can be displayed either by clicking a button in from the ribbon tab or by using keyboard shortcut(Ctrl+ Shift +S).
There are four places where I need to write my code.
The action method for the ribbon button called on click event.
public void btnInsertSignoff_Click(IRibbonControl control)
{
InsertSignoff();//This method displays the sign off form
}
The following code binds a key board shortcut with VBA code in the Addin Startup / Document Change event
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Globals.ThisAddIn.Application.KeyBindings.Add(WdKeyCategory.wdKeyCategoryCommand, "InsertSignOff ", Globals.ThisAddIn.Application.BuildKeyCode(WdKey.wdKeyControl, WdKey.wdKeyShift, WdKey. wdKeyS));
}
This link shows how to call VBA Code using Keyboard shortcuts.
http://www.wordbanter.com/showthread.php?t=31813
I followed this example but instead of VBA I did that in VSTO Addin Startup event but the second parameter " InsertSignOff " is a VBA method in step 4.
Wrote another method called InsertSignOff (Following exposing VSTO method to VBA).
[ComVisible(true)]
public interface IAddInAdapter
{
void InsertSignOff ();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddinAdapter : IAddInAdapter
{
public void InsertSignOff()
{
InsertSignoff();//This method displays the sign off form
}
}
This link explains how to expose VSTO code to VBA
http://msdn.microsoft.com/en-us/library/bb608604.aspx
Created a .dotm file in the user templates location “C:\Users\\AppData\Roaming\Microsoft\Templates” which contains the following VBA code
Sub InsertSignOff ()
Dim addIn As COMAddIn
Dim automationObject As Object
Set addIn = Application.COMAddIns("Wordaddin.AddinAdapter")
Set automationObject = addIn.Object
automationObject.InsertSignOff
End Sub
Hope this helps some one.
You can use the global key hook up method mentioned over here: https://learn.microsoft.com/en-us/archive/blogs/vsod/using-shortcut-keys-to-call-a-function-in-an-office-add-in

Handle File->New in Word 2007

I am writing a VSTO addin for Word 2007. When the user selects File->New, (or selects it from the quick access toolbar) I need to display a custom form instead of the standard new document dialog. How do I do this? I don't see an application event I can handle and I can't seem to find the buttont to add an event handler too.
Ok, just found it. You need to create a Ribbon xml and then add commands for those buttons. In this case the ribbon xml is
<commands>
<command idMso="FileNew" onAction="FileNewOverride"/>
<command idMso="FileNewDefault" onAction="FileNewOverride"/>
</commands>
and the code behind is
public void FileNewOverride(Office.IRibbonControl control, ref bool cancelDefault)
{
//do something
}
This how-to on MSDN shows you how to do it http://msdn.microsoft.com/en-us/office/dd361753.aspx