Re: getting Text to Speech to work in Word
The method described in the KB article mentioned above has you
starting up an Excel application to get speech. Here is a method to do
it just within Word:
The following applies to WordXP:
Go to the VBE (Alt+F11)
Add a reference in the normal project to Microsoft Speech Object
Library
(note: you must have installed the Speech portion of Excel for this
reference to be available.)
Create a new module
Insert the following code:
'Code Start =============================================
Dim speech As SpVoice
Sub SpeakText()
'
' Macro2 Macro
' Macro recorded 10/20/2003 by Mathew Heikkila
'
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(1,
ActiveDocument.Characters.Count).Text, _
SVSFlagsAsync + SVSFPurgeBeforeSpeak
End If
Do
DoEvents
Loop Until speech.WaitUntilDone(10)
Set speech = Nothing
End Sub
Sub StopSpeaking()
'used to interupt any running speach to text
'Dim speech As New SpVoice
On Error Resume Next
speech.Speak vbNullString, SVSFPurgeBeforeSpeak
Set speech = Nothing
End Sub
'Code End ====================================================
Now all you need are two toolbar buttons or menu items that run the
macros.
The SpeakText macro will speak the current selection, or the entire
document if nothing is selected.
The StopSpeaking macro will stop it.
Hope this can help anyone else who was as frustrated as me that Excel
had this feature but not Word.
Yes, its crude regarding the error trapping and using a global
variable. If anyone has a cleaner way, please post a reply. The whole
reason for this was to be able to stop the speaking. Simply creating a
new voice object and setting the PurgeBeforeSpeak flag did not work.
If the ASync flag is not turned on the in the speaktext macro, you
have to wait for it to finish before you can do anything else in Word.
Matt