VBScript - Text To Speech

Date Submitted Sat. Apr. 21st, 2007 7:04 PM
Revision 1 of 1
Helper TimboTheGreat
Tags speech | tts | VBSCRIPT | voice
Comments 0 comments
This script demonstrates the use of the Speech API via VBScript. Simply type in the word or phrase that you want to the computer to speak and click OK. Useful for vocalizing the results of a function or literally reading a text file to the user.

Note: To successfully run this code you MUST have the Speech API installed. The Speech API is installed during a complete Office install. If the required libraries are not present on your system, search Microsoft.com for the speech API SDK download. See this page for help on setting up Text To Speech: http://support.microsoft.com/kb/306902/

Option Explicit
'By Timbo

Const SVSFlagsAsync = 1
const SVSFPurgeBeforeSpeak = 2

Dim Speech


Sub Main
        Dim sText
        sText = InputBox("Enter the text you want the computer to say", "Text2Speech")
        sText = Trim(sText)
        If sText <> "" Then
                SpeakText sText
        End If
End Sub

Sub SpeakText(sText)
        On Error Resume Next
        Speech.Speak sText, SVSFlagsAsync + SVSFPurgeBeforeSpeak
                Sleep 100
        Loop Until Speech.WaitUntilDone(10)
End Sub

Sub StopSpeaking()
        On Error Resume Next
        Speech.Speak vbNullString, SVSFPurgeBeforeSpeak
        Set Speech = Nothing
End Sub

Sub CreateObjects
        Set Speech = CreateObject("SAPI.SpVoice")
        Set FSO = CreateObject("Scripting.FileSystemObject")
End Sub

Sub DestroyObjects
        Set Speech = Nothing
        Set FSO = Nothing
End Sub

Sub Sleep(nTimeout)
        WScript.Sleep nTimeout
End Sub

Sub Quit
End Sub

Tim M


There are currently no comments for this snippet.