VBScript - Text To Speech
5
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/
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
Dim FSO
CreateObjects
Main
DestroyObjects
Quit
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
Do
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
WScript.Quit
End Sub






There are currently no comments for this snippet.