Andri Yadi

Butterfly Effect
See also: Other Geeks@INDC

Speak Up Your Email - An Outlook Add-in using VSTO & Speech API

This is one of the live coding demo that I showed in OBA with VSTO v3 training at PT. Astra Honda Motor. Please refer to this posting to read the details about the training.

What I'm about to talk in this posting is an Outlook Add-in that can speak up your email, it's like you have a personal assistant that can read your email with nice intonation like human being. I'm gonna share with you in this posting about how to build it. It's simple yet fun.

To be able to speech an email message or any string, you need a Text-to-Speech (TTS) library. I won't discuss how to build it here, it's beyond my expertise, at least for now, so I'm gonna use existing one. Many free TTS or Speech Synthesizer available out there, such as: Microsoft Speech API SDK, Festival, festvox, FreeTTS, etc. For the sake of simplicity in term of getting the library and coding with managed code like VSTO, I use Microsoft Speech API (SAPI). If you use Windows XP or VISTA, SAPI has been available for free. The latest version is SAPI 5.3 delivered with Windows Vista, while Windows XP has SAPI 5.1.

To work with SAPI from managed code, you have two options: using native interfaces by using Interop to COM of SAPI 5.3, or using managed interface to SAPI 5.3 those are available in System.Speech* namespaces introduced in .NET Framework 3.0 or later. In this posting I use managed interface to simplify the code. Please refer to resources below for more details about Microsoft SAPI.

OK, lets get back to our topic. The steps to build the Add-in are:

1. Create New Project. On Project types list select Office, then select Outlook 2007 Add-in on templates list. Name the project as SpeakUpYourEmail or whatever you want, then click OK.

image

In your solution, you'll have these files. The ThisAddIn.cs file will be automatically opened.

image

2. To add Speech capability in your project, add reference to Microsoft Speech managed library. Right click to project name, click Add Reference, and at the Add Reference dialog select System.Speech.

image

 

3. Coding ThisAddIn.cs

The complete code for ThisAddIn.cs. Hopefully, it's self describing

using System;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Speech.Synthesis;
 
namespace SpeakUpYourEmail
{
    public partial class ThisAddIn
    {
        private SpeechSynthesizer synth;
        public SpeechSynthesizer Synthesizer
        {
            get { return this.synth; }
        }
 
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //Create new instance of SpeechSynthesizer
            synth = new SpeechSynthesizer();
            //Select Anna voice, more sexy and good intonation. 
            //Only available in Windows Vista.
            synth.SelectVoice("Microsoft Anna");
        }
 
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            if (synth != null)
            {
                synth.SpeakAsyncCancelAll();
                synth.Dispose();
                synth = null;
            }
        }
 
        public void StopTalking()
        {
            if (synth != null)
            {
                synth.SpeakAsyncCancelAll();
            }
        }
 
        public void StartTalking()
        {
            //Get the Explorer displaying mail list
            Outlook.Explorer activeExplorer = Application.ActiveExplorer();
            //Get the first selection of mails in mail list, cast it to MailItem
            Outlook.MailItem selectedMailItem = activeExplorer.Selection[1] as Outlook.MailItem;
 
            if (selectedMailItem != null)
            {
                String msgBody = selectedMailItem.Body; //get the mail body
 
                synth.SpeakAsyncCancelAll(); //First, cancel all speak
                synth.SpeakAsync(msgBody); //Then, speak it!!
            }
        }
 
        #region VSTO generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }        
        #endregion
    }
}

 

4. Add ribbon to place Speech control, such as button to start/stop speaking.

Right click to project name, Add New Item, select Ribbon (Visual Designer) template, name it DefaultRibbon.cs (or whatever you want), and click OK.

image

To control where the ribbon should be displayed in Outlook, select ribbon, then at Properties change RibbonType to whatever you want.In this case I choose Microsoft.Outlook.Mail.Read, so the ribbon will be displayed when you read an email in full read mode.

image

Add a button control to ribbon and set the properties like shown below

image

 

5. Code the ribbon

The complete code for ribbon:

using System;
using Microsoft.Office.Tools.Ribbon;
using System.Speech.Synthesis;
 
namespace SpeakUpYourEmail
{
    public partial class DefaultRibbon : OfficeRibbon
    {
        public DefaultRibbon()
        {
            InitializeComponent();
            this.Close += new EventHandler(DefaultRibbon_Close);
            this.buttonSpeech.Click += new EventHandler<RibbonControlEventArgs>(buttonSpeech_Click);
        }
 
        private void DefaultRibbon_Load(object sender, RibbonUIEventArgs e)
        {
        }
 
        private void DefaultRibbon_Close(object sender, EventArgs e)
        {
            //Stop speaking when the ribbon (reading mode) is closed
            Globals.ThisAddIn.StopTalking();
        }
 
        private void buttonSpeech_Click(object sender, RibbonControlEventArgs e)
        {
            //ThisAddIn methods & properties are accessible via Globals
            //Check SpeechSynthesizer state
            if (Globals.ThisAddIn.Synthesizer.State == SynthesizerState.Speaking)
            {
                //If it's speaking, stop it and change the button label
                Globals.ThisAddIn.StopTalking();
                buttonSpeech.Label = "Read it!";
            }
            else
            {
                //If it's paused or ready, start speaking 
                //and change the button label
                Globals.ThisAddIn.StartTalking();
                buttonSpeech.Label = "Stop!";
            }
        }
    }
}

 

6. Build the solution. If nothing goes wrong, debug it by pressing F5.

Then Microsoft Outlook will be appeared. Open your Inbox, double click to an email and Message dialog will be displayed. You'll notice there is a Add-Ins tab. Click it and you'll find Read it! button. Click the button, and you'll hear a nice sexy female voice reading your email.

image 

If the email to read is long enough, you can stop the reading by clicking that button again or just close the message dialog, the speaking will be stopped automatically. What a pleasure having a sexy assistant reading your email huh! :)

As a conclusion of this posting, VSTO allows us to build unlimited possibility of OBA, from the simple one like example in this posting, until the complex ones like my company product PortMan. For more details about VSTO, go to: VSTO Developer Center

You can download the VS 2008 solution of this example at: http://dycode.com/files/folders/codesamples/entry92.aspx

Have a nice try and let me know your comments.

 


Microsoft Speech API resources:

Exploring New Speech Recognition And Synthesis APIs In Windows Vista

System.Speech code samples

Speech Synthesis Example

Developing for Speech

A Fun Windows Form Application

 
Share this post: | | | |

Comments

narn said:

This is cool coy.

# February 18, 2008 11:58 AM

andriyadi said:

Thanx bro...

Simple but useful & fun

# February 18, 2008 2:54 PM

angnat said:

Keren banget bos,aq ngeliat pas demonya pas di Microsoft Indonesia tgl 29/2/2008

# March 4, 2008 10:03 PM

zxevil163 said:

7EouRF Hi from Russia!

# March 17, 2008 7:30 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 
Are you human?:  


Enter the numbers above: