Multiple Apps

Using Microsoft's Speech Object Library to Perform Text To Speech

Ease of Use

Easy

Version tested with

2003, Vista 

Submitted by:

Oorang

Description:

As some of you have discovered Excel 2003 has a nifty little method to perform text to speech (make the computer talk). This method (Excel.Application.Speech.Speak) is very easy to use. However no such method is made available in Access, Word, etc. This example will show you how to still use text to speech in your applications without resorting to referencing Excel. 

Discussion:

The most obvious use is handicap accessibility, but I have also found this useful for a welcome to accompany a splash screen, notifications I want to have a non-standard sound prompt etc. 

Code:

instructions for use

			

Option Explicit '-------------------------------------------------------------------------------- ' Procedure : Example ' Author : Aaron Bush ' Date : 9/9/2008 ' Purpose : To demonstrate simple usage of the Microsoft Speech Object Library ' Revisions : Aaron Bush 9/9/2008 Initial Creation. '-------------------------------------------------------------------------------- Public Sub Example() Const strTitle As String = "Speech Demo - Message " Dim eRslt As VBA.VbMsgBoxResult VoiceBox "Though I speak with the tongues of men and of angels, and " & _ "have not charity I am nothing.", vbInformation, _ strTitle & "1" Do 'From Sid Meier's Alpha Centauri, please don't sue me Sid. eRslt = VoiceBox("""Abort, Retry, Fail?"" was the phrase some " & _ "wormdog scrawled next to the door of the Edit " & _ "Universe project room. And when the new " & _ "dataspinners started working, fabricating their " & _ "worlds on the huge organic comp systems, we'd " & _ "remind them: if you see this message, always " & _ "choose ""Retry.""", vbQuestion + vbAbortRetryIgnore, _ strTitle & "2") Select Case eRslt Case VbMsgBoxResult.vbAbort VoiceBox "Abortion takes a life.", title:=strTitle & "3" End Case VbMsgBoxResult.vbRetry VoiceBox "Hell is repetition.", title:=strTitle & "4" Case VbMsgBoxResult.vbIgnore VoiceBox "There is no problem so great it can not be ignored." _ , title:=strTitle & "5" Exit Do End Select Loop While eRslt = vbRetry End Sub '-------------------------------------------------------------------------------- ' Procedure : VoiceBox ' Author : Aaron Bush ' Date : 9/9/2008 ' Purpose : Provides a message box that uses the Microsoft Speech API (SAPI) ' to speak the contents of the message box. This has the advantage ' of working across multiple applications without alteration (as ' opposed to Excel.Application.Speech.Speak). ' Input(s) : All inputs are the exact same as the standard MsgBox Function. ' Output(s) : Output will be an indicator of the users response. ' Revisions : Aaron Bush 9/9/2008 Initial Creation. '-------------------------------------------------------------------------------- Public Function VoiceBox(ByVal prompt As String, _ Optional ByVal buttons As VbMsgBoxStyle, _ Optional ByVal title As String, _ Optional ByVal helpFile As String, _ Optional ByVal context As Long) As VbMsgBoxResult 'For Early Bound object set a reference to Microsoft Speech Object Library: 'C:\Windows\System32\Speech\Common\sapi.dll 'Dim spv As SpeechLib.SpVoice 'Set spv = New SpeechLib.SpVoice 'Late Bound example follows: Dim spv As Object Set spv = CreateObject("SAPI.SpVoice") spv.Speak prompt, SVSFlagsAsync VoiceBox = Msgbox(prompt, buttons, title, helpFile, context) Set spv = Nothing End Function

How to use:

  1. Open VBE by pressing Alt-F11.
  2. From the insert menu insert a standard module (will just say "Module").
  3. Paste Code
  4. Set a reference to Microsoft Speech Object Library
  5. Run Example Sub
 

Test the code:

  1. Try the example sub, instructions above.
 

Sample File:

TestSpeech.zip 12.71KB 

Approved by mdmackillop


This entry has been viewed 165 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express