Thursday, June 24, 2010

VBA : How to convert text file to speech (audio) using VBA

Text to Speech using Excel VBA : Audio/Speech from input file

If you want to spell out the content of text file using VBA you can do it as shown below:


Sub Speech_FromFile_Example()

Dim oVoice As SpVoice                               ' Voice Object
Dim oVoiceFile As SpFileStream                      ' File Stream Object
Dim sFile As String                                 ' File Name

Set oVoice = New SpVoice
Set oVoiceFile = New SpFileStream

' --------------------------------------------------------------
' Code for http://vbadud.blogspot.com
' --------------------------------------------------------------

oVoice.Speak "This is an example for reading out a file"

sFile = "C:\ShasurData\ForBlogger\SpeechSample.txt"

oVoiceFile.Open sFile

oVoice.SpeakStream oVoiceFile

End Sub

The above code creates a filestream and reads the text file and the Voice object speaks it out!

The code requires Microsoft Speech Object Library (see figure below)



See also:

Voice Messages in VBA

3 comments:

  1. Good but how to save mp3 file of that text



    Thanks
    sameer

    sameertanpure29390@gmail.com

    ReplyDelete
  2. Thank you for sharing this code snippet.

    I would really like to know how to save the output of speech to a MP3 file.

    ReplyDelete
  3. I get an error about the function being restricted or using an automation interface not supported by VBA...

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.