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

2 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

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...

Visual Basic for Applications (VBA) Forum (recent threads)

CodeKeep VBA Feed

Visual Studio Tools for Office Forum (recent threads)

Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.

Office Business Applications (OBA) Team Blog

MSDN Code Gallery Published Resources For Tag VSTO

microsoft.public.vsnet.vstools.office Google Group