Setting the Active Excel Worksheet using Interop and Speech Recognition with C#

I’ve been looking at using speech recognition to manipulate Excel 2013. One interesting task I solved is setting the active worksheet. For example, the command “sheet one” will have the same effect as clicking on the Sheet1 tab near the bottom of an open spreadsheet.


In order to do this, the client machine must have a lot of somewhat confusing prerequisite libraries related to Excel interop (to manipulate Excel) and Speech (to recognize commands) installed.

Although it’s not essential, I implemented everything as an Excel add-in, mostly so I could have some UI to display logging messages and turn speech on/off. To develop the add-in using Visual Studio 2012 I needed to download and install the “Microsoft Office Developer Tools for Visual Studio 2012 ENU”. To develop the speech components I needed to install three 32-bit (not 64! 64-bit doesn’t work with Excel interop — that was a pain to discover) Microsoft Speech Platform 11 libraries: the SDK (to create in VS), the Runtime (to use speech on my client machine), and English Language pack MSSpeech_SR_en-US_TELE.msi for recognition. Also, although I didn’t use it, I downloaded and installed MSSpeech_TTS_en-US_Helen.msi for speech synthesis (to make the computer speak with Helen’s voice).

Just getting a basic add-in working was no small chore. See my blog post at for the ugly details. In this case the add-in is essentially a WinForm with a CheckBox control (“SR” in the image) to toggle speech on and off.

The key code is listed below. The code recognizes a command like “sheet two”, parses the “two” out, converts it to numeric 2 and then uses the Excel.Worksheet Activate method. What is not shown in the code is that I programmatically insert new worksheets. The first sheet is always at the last index in the Worksheets collection. The indexing for the other sheets is a bit tricky and the code is the best explanation.

namespace Excelligence
  public partial class ExcelligenceUserControl : UserControl
    static int sheetNum = 0; // used to create a new sheet for each clustering result
    static SpeechRecognitionEngine sre; // to recognize spoken commands like "cluster 3"
    //static SpeechSynthesizer ss; // not right now . . 
    public ExcelligenceUserControl()
      System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-us");
      sre = new SpeechRecognitionEngine(ci);
      sre.SpeechRecognized += new EventHandler(sre_SpeechRecognized);

      Choices numbers = new Choices();
      Choices sheets = new Choices();
      GrammarBuilder gb_Sheets = new GrammarBuilder();
      Grammar g_Sheets = new Grammar(gb_Sheets);

    void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
      string txt = e.Result.Text;
      float conf = e.Result.Confidence;

      if (conf < 0.65) return;
      listBox1.Items.Add("Recognized " + txt); 
      string[] tokens = txt.Split(' ');
      int n = 0;
      if (txt.IndexOf("Sheet one") >= 0 ||
        txt.IndexOf("Sheet two") >= 0 ||
        txt.IndexOf("Sheet three") >= 0 ||
        txt.IndexOf("Sheet four") >= 0)
        n = Globals.ThisAddIn.Application.Worksheets.Count;
        listBox1.Items.Add("there are " + n + " sheets");
        string[] twoWords = txt.Split(' ');
        string sheetNumAsString = twoWords[1];
        int sn = NumberFromString(sheetNumAsString);
        listBox1.Items.Add("sn = " + sn);
        if (sn > n) return;

        int idx;
        if (sn == 1) idx = n; // weirdness! "Sheet1" is always last
        else idx = sn - 1; // ??? (but works)

        Excel.Worksheet ws = Globals.ThisAddIn.Application.Worksheets[idx];
        ws.Visible = Excel.XlSheetVisibility.xlSheetVisible;
    } // sre_SpeechRecognized

    static int NumberFromString(string numAsString)
      if (numAsString == "one") return 1;
      else if (numAsString == "two") return 2;
      else if (numAsString == "three") return 3;
      else if (numAsString == "four") return 4;
      return -1; // error
      //return 3;

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
      if (checkBox1.Checked == true) // turn SR on
      else if (checkBox1.Checked == false) // turn SR off
    // etc.
This entry was posted in Machine Learning. Bookmark the permalink.

2 Responses to Setting the Active Excel Worksheet using Interop and Speech Recognition with C#

  1. kiquenet says:

    Great !! What’s is your twitter ?

Comments are closed.