Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ms-word

vsto

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

like image 684
Peter Avatar asked Apr 14 '10 07:04

Peter


People also ask

How do you add Vsto to Word?

On the File menu, point to New, and then click Project. In the templates pane, expand Visual C# or Visual Basic, and then expand Office/SharePoint. Under the expanded Office/SharePoint node, select the Office Add-ins node. In the list of project templates, select a Word VSTO Add-in project.

Which key do you press to show shortcuts for the ribbons and commands?

Use KeyTips to access the ribbon Press the Alt key. You see the little boxes called KeyTips over each command available in the current view.

What is toggle ribbon shortcut?

ALT or F10 to select the active tab, and then LEFT ARROW or RIGHT ARROW. Move to another group on the active tab. ALT or F10 to select the active tab, and then CTRL+RIGHT ARROW or LEFT ARROW to move between groups. Minimize (collapse) or restore the ribbon. CTRL+F1.


2 Answers

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.

  1. 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 
    }
    
  2. 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.

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

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

like image 152
Kiru Avatar answered Sep 19 '22 14:09

Kiru


You can use the global key hook up method mentioned over here: https://docs.microsoft.com/en-us/archive/blogs/vsod/using-shortcut-keys-to-call-a-function-in-an-office-add-in

like image 23
Abhi Avatar answered Sep 20 '22 14:09

Abhi