7

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

Peter
  • 1,292
  • 4
  • 15
  • 33
  • You could try a global key hook - http://www.liensberger.it/web/blog/?p=207 and use the handle of the Excel application (ThisAddIn.ExcelApplication.Hwnd). – Mikael Svenson Apr 14 '10 at 08:47
  • Thanks, Mikael, I will try that. I would still be a bit disappointed that there was no simple way in the Word 2007/2010 user interface that will give any user the ability to set their own short cuts - maybe they won't like the one that I choose. – Peter Apr 14 '10 at 12:52
  • I gave the above solution a try, but it sets a global key (which is what it says), and it fires my ribbon button event even when Word does not have the focus, so it's a bit intrusive. – Peter May 04 '10 at 19:52
  • If you've got it hooked globally, just test to make sure the context (active application, active window, not in header/footer etc), is correct. Sounds like if you've got a global hook that far along, you're 90% there. – DarinH Apr 08 '11 at 20:57
  • Thanks, drventure, I'll give that a try when I pick up that part of the app. again. For the moment, Word 2007/2010 Alt key plus quick access toolbar works quite nicely. – Peter May 04 '11 at 10:14

2 Answers2

10

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.

Kiru
  • 3,489
  • 1
  • 25
  • 46
  • 2
    I was with you until you need to edit files on the machine of every user who wants to use this - I was assuming OP required a generic solution that can simply be coded. This solution needs a large caveat - "requires editing files with VBA code in them on the machine of every user" – PandaWood Jan 29 '16 at 00:06
  • 1
    taking into account that by default office is blocking macros this doesn't seem to be the best scenario. – DanielV Aug 13 '20 at 08:03
1

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

EylM
  • 5,967
  • 2
  • 16
  • 28
Abhi
  • 4,872
  • 1
  • 22
  • 20