开发者

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

开发者 https://www.devze.com 2022-12-26 14:24 出处:网络
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 th

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 k开发者_Go百科eyboard 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.

  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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消