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

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

Related

word object model - active document event when clicking on FILE

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).

Assign Outlook Categories to a RibbonMenu with buttons

I am currently creating a Outlook Add-in and need to include the Outlook Catagorize Menu to my add-in.
With this Foreach I´m Creating and adding the Buttons manually into the "Kategorisieren"-RibbonMenu and naming them the same as the catagory names. Now I´m trying to copy the color-images of the categories to the button Image which works by assigning them with the right OfficeImageId. Nevertheless there are some catagories which colors are not described as OfficeImageIds, but rather like objects f. e. the color peach as an object of OlCategoryColor.OlCategoryColorPeach . The Problem is that I cant use them as the others by assigning them to the button.Image or OfficeImageId. How do I get these little Icons in the right Color next to my buttons.
The next Task would be assigning the Category to the Appointment by clicking on the right button.
Categories categories = Globals.TerminAddIn.Application.Session.Categories;
foreach (Category category in categories)
{
var button = this.Factory.CreateRibbonButton();
this.Kategorisieren.Items.Add(button);
button.Label = category.Name;
}
this.Kategorisieren.ControlSize = RibbonControlSize.RibbonControlSizeLarge;
this.Kategorisieren.OfficeImageId = "CategorizeMenu";
this.Kategorisieren.ShowImage = true;
I'd recommend using the ribbon XML with callbacks where you can define the button's images and labels. At any point in time, you can update the buttons list or change categories with colors. Read more about this approach in the Walkthrough: Create a custom tab by using Ribbon XML article.
When you customize the Ribbon UI by using callback procedures, for each of the callbacks the add-in implements, the responses are cached. 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 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)

Handling window focus events in a gnome shell extension

I am developing a gnome shell extension for Gnome 3.4. My extension needs to capture the window events if any editable text is focused in/out.
global.stage.connect('notify::focus-key', Lang.bind(this, this._myHandler));
did not work for me.
Here is a simple use-case: whenever user clicks on firefox search box, I want my handler to be run.
Thanks for any help,
Selcuk pointed me this question, so in order to have this answered here for future search.
The library that would allow to set a global-desktop listener to focus changes is libatspi (the client-side library of GNOME accessibility framework). You could use directly C, pyatspi2 (python manual bindings) or gobject-introspection based bindings (ie, javascript). So a small javascript program that prints name:role_name of the focused object each time the focus change would be:
const Atspi = imports.gi.Atspi;
function onChanged (event) {
log(event.source.get_name() + ',' + event.source.get_role_name());
}
Atspi.init();
let atspiListener = Atspi.EventListener.new(onChanged);
atspiListener.register("object:state-changed:focused");
Atspi.event_main();
In any case, for code examples, you could take a look to recently added focus/caret tracking feature on gnome-shell magnifier (small-size example using javascript) or Orca (GNOME screen reader, big-size example, uses pyatspi2).
libatspi reference here: https://developer.gnome.org/libatspi/
gnome-shell magnifier code here: https://git.gnome.org/browse/gnome-shell/tree/js/ui/magnifier.js
you cannot do this.
application text entry widgets do not fall under the scope of the window manager, so you cannot access their contents, or whether or not they received focus.

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).

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