Tag Archives | speak

Using VBA with Speech

If you program using VBA in Excel there might come a time when you would like the application to speak to the user. The good news is you can use the Application.Speech.Speak method to convert a string to Speech. This works for all versions from 2003 and later.

In the following example, Microsoft Excel plays back “Hello Bob”. I am assuming speech features have been installed on the host system.

Sub SayHello()
    Application.Speech.Speak "Hello Bob"
End Sub

 

There is a second argument of the Speak method that defines either synchronous or asynchronous. True will cause the Text to be spoken asynchronously (the method will not wait of the Text to be spoken). False will cause the Text to be spoken synchronously (the method waits for the Text to be spoken before continuing). The default is False.

The following code illustrated how this works:

Sub DemoSpeech()
    Application.Speech.Speak "This is spoken then code runs"
    MsgBox "This is displayed after the previous text is spoken"
    Application.Speech.Speak "Spoken and the next code runs", True
    MsgBox "This is displayed while the previous text is spoken"
End Sub

 

Another feature that is useful is the ability of Excel to speak the contents of the cell after the user has finished entering data (on pressing Enter).

Sub EnableSpeech()
   Application.Speech.SpeakCellOnEnter = True
End Sub

 

To turn off the Speak On Enter set the value to False.