Macro of the Day: Say what you want and get it! (inside Word, Excel, and PowerPoint)

Today's macro of the day is a cool trick involving another free tool from Microsoft called Search Commands. Search Commands helps you find commands, options, wizards and other cool stuff in Office 2007 applications (Word, Excel, and PowerPoint). It's like search, but instead of searching content, it searches the tasks that are available in Microsoft Office.

Search Commands is built in the newly developed Office Labs team. They build software prototypes that are to the software world what concept cars to the automotive world.

A year or so ago, when I met up with some of the folks on the Search Commands team I fell in love with the Office add-in.

As implemented today without using the macro of the day, Search Commands is a new "tab" at the top of Office 2007 applications. If you want to find something, you click the tab, click inside the edit box, type what you're looking for, and amazingly quickly it shows you exactly what you're looking for as a set of search results in the Office Ribbon.

Well ... Being a speech guy, I thought, why can't I just say what I want and get it, right away, in a single command. That's what today's Macro of the Day is.

Basically, today's macro listens for a series of commands, like "Search commands for [...]", and will automatically switch to the right tab, place focus in the edit control, type what you said in the [...] section, and allow you to pick from the results. I no longer have to care at all where features are on the ribbon tabs. I just say what I want and get it, in one utterance.

Let's take a look at the commands in the macro.

The first command, listens for "Search Commands" if the application that the user is currently using is either Word, Excel, or PowerPoint. Then, if WSR Macros hears that, it'll set the text feedback and send some keys to the application. Those keys (<alt-y> s c o <ctrl-y>) are the magic keyboard shortcuts to put focus into the right field. the <ctrl-a> selects all the text if there's any text already there, and then the {delete} deletes that text. This command helps you get to the right tab, and puts focus in the right field, but only does part of the task for you...

Let's take a look:

<command>
  <condition operator="or">
    <appIsInForeground processName="winword.exe"/>
    <appIsInForeground processName="excel.exe"/>
    <appIsInForeground processName="powerpnt.exe"/>
  </condition>
  <listenFor>search commands</listenFor>
  <setTextFeedback speak="false">Search for a command</setTextFeedback>
  <sendKeys>%y</sendKeys>
  <waitFor seconds=".25"/>
  <sendKeys>sco^a{delete}</sendKeys>
</command>

The next command, listens for the same sorts of things, but it also allows you to say what you want, and it'll do the tab switching, as well as filling in the field to do the search for you. Take a look:

<command>
  <condition operator="or">
    <appIsInForeground processName="winword.exe"/>
    <appIsInForeground processName="excel.exe"/>
    <appIsInForeground processName="powerpnt.exe"/>
  </condition>
  <listenFor>search for ?a command [...]</listenFor>
  <listenFor>search for ?a command [...] [number]</listenFor>
  <listenFor>search commands ?for [...]</listenFor>
  <listenFor>search commands ?for [...] [number]</listenFor>
  <setTextFeedback speak="false">Search for command: {[...]}</setTextFeedback>
  <sendKeys>%y</sendKeys>
  <waitFor seconds=".25"/>
  <sendKeys>sco^a</sendKeys>
  <waitFor seconds=".25"/>
  <insertText>{[...]}</insertText>
  <waitFor seconds=".25"/>
  <sendKeys>^a</sendKeys>
  <sendKeys>{[number]}</sendKeys>
</command>

And finally, there's another command that listen's for you to say the same sorts of things, but then just tells you that it can't do what you're asking. This is generally a good GUI/VUI design, to prevent you from trying to use the same voice command in applications that don't currently support it (like Outlook).

Here's that command:

<command>
  <condition operator="not">
    <condition operator="or">
      <appIsInForeground processName="winword.exe"/>
      <appIsInForeground processName="excel.exe"/>
      <appIsInForeground processName="powerpnt.exe"/>
    </condition>
  </condition>
  <listenFor>search commands</listenFor>
  <listenFor>search commands ?for [...]</listenFor>
  <listenFor>search commands ?for [...] [number]</listenFor>
  <listenFor>search for ?a command [...]</listenFor>
  <listenFor>search for ?a command [...] [number]</listenFor>
  <setTextFeedback style="warning" speak="false">What was that?</setTextFeedback>
</command>

There's one more command in the macro that's pretty useful. It's a command that turns the user's voice saying a number between 1 and 9 into a digit key press, as opposed to a textual insertion in the document. This is important when using Search Commands because if I use the 2nd command above, and say "Search Commands for Background color", Search Commands will do the right thing, showing the results in the ribbon, and allow the user to press 1 for the first command, 2 for the second command, etc. But ... The speech system on Vista by default doesn't know that, so this command listens for digits, and presses the keys when in Word, PowerPoint, and Excel, instead of allowing you to insert text...

Here's what that command looks like:

<command>
  <condition operator="or">
    <appIsInForeground processName="winword.exe"/>
    <appIsInForeground processName="excel.exe"/>
    <appIsInForeground processName="powerpnt.exe"/>
  </condition>
  <listenFor>[number]</listenFor>
  <setTextFeedback speak="false">{[number]}</setTextFeedback>
  <sendKeys>{[number]}</sendKeys>
</command>

So ... If we put it all together, here's the full macro:

<speechMacros>

  <command>
    <condition operator="or">
      <appIsInForeground processName="winword.exe"/>
      <appIsInForeground processName="excel.exe"/>
      <appIsInForeground processName="powerpnt.exe"/>
    </condition>
    <listenFor>search commands</listenFor>
    <setTextFeedback speak="false">Search for a command</setTextFeedback>
    <sendKeys>%y</sendKeys>
    <waitFor seconds=".25"/>
    <sendKeys>sco^a{delete}</sendKeys>
  </command>

  <command>
    <condition operator="or">
      <appIsInForeground processName="winword.exe"/>
      <appIsInForeground processName="excel.exe"/>
      <appIsInForeground processName="powerpnt.exe"/>
    </condition>
    <listenFor>search for ?a command [...]</listenFor>
    <listenFor>search for ?a command [...] [number]</listenFor>
    <listenFor>search commands ?for [...]</listenFor>
    <listenFor>search commands ?for [...] [number]</listenFor>
    <setTextFeedback speak="false">Search for command: {[...]}</setTextFeedback>
    <sendKeys>%y</sendKeys>
    <waitFor seconds=".25"/>
    <sendKeys>sco^a</sendKeys>
    <waitFor seconds=".25"/>
    <insertText>{[...]}</insertText>
    <waitFor seconds=".25"/>
    <sendKeys>^a</sendKeys>
    <sendKeys>{[number]}</sendKeys>
  </command>

  <command>
    <condition operator="or">
      <appIsInForeground processName="winword.exe"/>
      <appIsInForeground processName="excel.exe"/>
      <appIsInForeground processName="powerpnt.exe"/>
    </condition>
    <listenFor>[number]</listenFor>
    <setTextFeedback speak="false">{[number]}</setTextFeedback>
    <sendKeys>{[number]}</sendKeys>
  </command>

  <command>
    <condition operator="not">
      <condition operator="or">
        <appIsInForeground processName="winword.exe"/>
        <appIsInForeground processName="excel.exe"/>
        <appIsInForeground processName="powerpnt.exe"/>
      </condition>
    </condition>
    <listenFor>search commands</listenFor>
    <listenFor>search commands ?for [...]</listenFor>
    <listenFor>search commands ?for [...] [number]</listenFor>
    <listenFor>search for ?a command [...]</listenFor>
    <listenFor>search for ?a command [...] [number]</listenFor>
    <setTextFeedback style="warning" speak="false">What was that?</setTextFeedback>
  </command>

  <numbers name="number" start="1" stop="9"/>

</speechMacros>

Remember, to take advantage of the the power of Search Commands and WSR Macros combined, you'll have to download both, and install this macro in your Speech Macros directory.

Related links:

Let us know what you think!