Word text to speech
|
|
Although it is not a function I
personally have had use for, the ability to have Word read out the text
is nevertheless a function that many Word users find useful. On
researching the problem, I noted that Microsoft's own knowledge base
article -
http://support.microsoft.com/?kbid=287120
invokes the use of Excel to provide the speech
function within Word. This tutorial takes that article a step further to
provide a practical application within Word
The macros here use the
Microsoft Speech Object Library from
within Word to speak either the full document or a selected block of
text.
I take no credit for the macros beyond
reproducing them here. The code was produced by Mathew Heikkila in
response to a
newsgroup question back in 2003.
From Word (2002/3) Start the VBA
Editor (Alt+F11)
Add a reference in the normal project
to Microsoft Speech Object Library (Tools > References...) - see below
Locate the reference (first picture) and add a check mark. It will then
join the other checked items (second picture). |
| Note: |
You must have installed the Speech portion of Excel
for the Microsoft Speech Object Library to be available to the VBA
editor. |
|

 |
| |
Create a new module and call it TextToSpeech |
|
 |
| |
Copy and paste the following macro code into the module
you have created, save and close the macro editor. |
|
Dim speech as SpVoice
'Don't overlook this line!
Sub SpeakText()
'Based on a macro 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(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 |
|
Note: |
Do not overlook the first line -
Dim speech as SpVoice - or the macro to
stop speaking will not work. |
| |
Create a
personal toolbar in the default normal.dot template, and add the two
macros to that toolbar. Edit their names to something manageable like
those in the following illustration. Clicking 'Speak Text' will read
either the selected text block, or, if no text is selected, the whole
document. |
|

|
| |
It is unlikely that you will need the toolbar permanently
displayed, but it is easy to toggle the toolbar on/off with a further
short macro attached to a button on the standard toolbar. The code for
that would be as follows. This code can easily be adapted to pop up
infrequently used toolbars, as in the following screen shot. |
|
Sub SpeechBar()
With CommandBars("Text to Speech")
.Visible = Not .Visible
End With
End Sub |
| |
 |
| Notes: |
For more
information about installing macros from code listings and adding them
to toolbars, use
this link
While this macro will work in Word 2007, the toolbar cannot be added in
the same way. You can, with access to an earlier Word version create the
toolbars as an add-in or you
could add the start and stop macros to the QAT (Quick Access Toolbar). |