Wednesday, 26 September 2018

Start Text To Speech Functionality in Microsoft Word 2007

By Default, There is no Feature in Word To Start Text to Speech In Microsoft Word 2007. You have to make a Macro to enable TTS.The macros here use the Microsoft Speech Object Library from within Word to speak either the full document or a selected block of text.

From Word - Start the VBA Editor (Alt+F11)

Add a reference in the normal project to Microsoft Speech Object Library (Tools > References...)

Open Visual Basic From Word Document inside Developer Tab. On the Normal Project, Add a Module and write following code to it:


Option Explicit
Dim speech As SpVoice
Dim i As Integer

Sub SpeakText()
On Error Resume Next
If i = 0 Then
  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
Else
  If i = 1 Then
  speech.Resume
  i = 0
  End If
End If
End Sub

Sub StopSpeaking()
On Error Resume Next
speech.Speak vbNullString, SVSFPurgeBeforeSpeak
Set speech = Nothing
i = 0
End Sub

Sub PauseSpeaking()
On Error Resume Next
If i = 0 Then
  speech.pause
  i = 1
Else
  If i = 1 Then
  speech.Resume
  i = 0
  End If
End If
End Sub


You will see these options in View > Macros..


Happy Coding !!!

No comments:

Post a Comment

SQL Audits

1. sys.server_audits What it is: Lists all server-level audit objects . An audit is the top-level object that defines: Where to wri...