Reputation:
Is there any way to create a VBA code to read a word document via audio? as if the computer was reading the whatever is on the document. (of course all text).
Upvotes: 1
Views: 2925
Reputation: 1
Be careful if you are using Windows 10 - I had two libraries with the same name "Microsoft Speech Object Library" - make sure you select the one which points to Windows/system32/Speech/Common/sapi.dll
Upvotes: 0
Reputation: 1189
I found this searching on google. http://vbadud.blogspot.com/2010/06/vba-how-to-convert-text-file-to-speech.html
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
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 code requires Microsoft Speech Object Library
VBA macro for Word:
Dim speech as SpVoice
Sub SpeakText()
On Error Resume Next
Set speech = New SpVoice
If Len(Selection.Text) > 1 Then 'speak selection
speech.Speak Selection.Text
SVSFlagsAsync + SVSFPurgeBeforeSpeak
Else 'speak whole document
speech.Speak ActiveDocument.Range(0, ActiveDocument.Characters.Count).Text
SVSFlagsAsync + SVSFPurgeBeforeSpeak
End If
Do
DoEvents
Loop Until speech.WaitUntilDone(10)
Set speech = Nothing
End Sub
Sub StopSpeaking()
'Based on a macro by Mathew Heikkila
'used to interrupt any running speech to text
On Error Resume Next
speech.Speak vbNullString, SVSFPurgeBeforeSpeak
Set speech = Nothing
End Sub
Upvotes: 1