简体   繁体   English

如何将VSTO C#代码连接到功能区和/或键盘快捷方式

[英]How to connect VSTO C# code to the ribbon and/or keyboard shortcuts

I have just started migrating to C# from VBA. 我刚刚开始从VBA迁移到C#。 I have about 100 VBA macros to convert, of which about 25 are either in a custom Ribbon entry or mapped to a keystroke, such as alt-P. 我有大约100个VBA宏要转换,其中大约25个在自定义功能区条目中或映射到按键,例如alt-P。

The only reference I can find says I need to retain some VBA code in order to do this. 我可以找到的唯一参考资料说,我需要保留一些VBA代码才能执行此操作。 Such a solution is totally unacceptable, I have wanted to dispense with VBA entirely. 这样的解决方案是完全不能接受的,我一直想完全放弃VBA。 I can't help thinking this is like having to keep a bale of hay in the frunk of your Tesla to feed the horse. 我忍不住想这就像必须在特斯拉的草丛中留一捆干草来喂马。

Is there any better way? 有什么更好的办法吗?

Press the Alt key to get the keytip reference. Alt键以获取按键提示参考。 As per the screenshot below, I want to reference the Script Help ribbon which is Y2 . 根据下面的屏幕截图,我想引用Script Help功能区Y2

屏幕截图功能区

Then press Alt + Y + 2 to show the control keytip control references for that ribbon. 然后按Alt + Y + 2以显示该功能区的控件按键控件参考。 Now I want to reference the Clean Data button which is Y7 . 现在,我要引用“ Clean Data按钮Y7

屏幕截图按钮

Then you can use SendKeys to press those keytip references. 然后,您可以使用SendKeys按下这些按键提示引用。 By adding the following code. 通过添加以下代码。

    public void CallButtonFromAnotherRibbon()
    {
        try
        {
            SendKeys.Send("%");
            SendKeys.Send("Y");
            SendKeys.Send("2");
            SendKeys.Send("%");
            SendKeys.Send("Y");
            SendKeys.Send("7");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

Microsoft Documentation for KeyTips Microsoft提示文档

Microsoft Documentation for SendKeys Microsoft SendKeys文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM