2

I have just started migrating to C# from VBA. 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.

The only reference I can find says I need to retain some VBA code in order to do this. Such a solution is totally unacceptable, I have wanted to dispense with VBA entirely. 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?

Community
  • 1
  • 1
casgage
  • 529
  • 1
  • 7
  • 18

1 Answers1

0

Press the Alt key to get the keytip reference. As per the screenshot below, I want to reference the Script Help ribbon which is Y2.

screenshot ribbon

Then press Alt + Y + 2 to show the control keytip control references for that ribbon. Now I want to reference the Clean Data button which is Y7.

screenshot buttons

Then you can use SendKeys to press those keytip references. 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 Documentation for SendKeys

aduguid
  • 3,099
  • 6
  • 18
  • 37