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

前端 未结 2 525
猫巷女王i
猫巷女王i 2020-12-06 13:17

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)

相关标签:
2条回答
  • 2020-12-06 13:30

    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.

    0 讨论(0)
  • 2020-12-06 13:43

    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

    0 讨论(0)
提交回复
热议问题