{Word} Highlight or Underline word while Speaking
I was hoping to make this simple (use Word) instead of making a VB project.
I have "text to speak" implemented with just a few lines of code.
The primary loop for speaking (after Text selection) is:
Code:
Do
DoEvents
Loop Until speech.WaitUntilDone(10)
Using Words Macro recorder, I can move from word to word using:
Code:
Selection.MoveRight Unit:=wdWord, Count:=3
I would like each word to be highlighted or underlined as each word is read.
This will require the old word to be un-highlighted or un-underlined so that the
current word being read is emphasized.
Not sure whether placing a counter is the Do/Loop will somehow allow word to reference where (which word is being read) the cursor is at in the document.
===============================
Anyone have any ideas??
Thanks
David
Re: {Word} Highlight or Underline word while Speaking
here is some sample to move the selection along the words and underline them
as the selection moves underlining is bit irrelevant
this in no way takes account of the speed of the speaking but that should be able to be adjusted to suit
vb Code:
Do
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Range.Font.Underline = True
t = Timer
Do Until Timer - t > 2
DoEvents
Loop
Selection.Range.Font.Underline = False
Selection.MoveRight Unit:=wdWord, Count:=1
Loop
position cursor at beginning of text before running
Re: {Word} Highlight or Underline word while Speaking
westconn1:
Thanks for the code example. Will see what happens.
Speech loads the entire document or selection into its buffer using something similar to a textbox
Code:
With tbText
.SelStart = 0
.SelLength = Len(.Text)
End With
So the problem becomes knowing where the speech is in "its" buffer in relationship to Words buffer (my guess it has one), and correlating the two, so that as the document is being read, each word is emphasized (underlined, bolded, highlighted, etc.)
Currently the way "text to speak" is implement it just reads the document or selection from the Speech buffer and the words displayed on the screen (in Word) just remain at their original cursor selection point.
Re: {Word} Highlight or Underline word while Speaking
westconn1:
Using your code with a few modifications, this works.
Speaking still is a little slow, but otherwise should serve Mom's purposes.
Code:
' Do
For Each vWord In ActiveDocument.Words
' If ActiveDocument.Bookmarks.Exists("\EndofDoc") Then Exit Do
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Range.Font.Underline = True
speech.Speak Selection.Text, SVSFlagsAsync + SVSFPurgeBeforeSpeak
t = Timer
Do Until Timer - t > 1 '1 was 2
DoEvents
Loop
Selection.Range.Font.Underline = False
Selection.MoveRight Unit:=wdWord, Count:=1
Next
' Loop
Re: {Word} Highlight or Underline word while Speaking
you can make the value less than 1 if you want faster, try .8 or something
Re: {Word} Highlight or Underline word while Speaking
westconn1:
Tried that (.4 or .5 seems to work OK), and speeds up reading, but pronoucement of the words
looping one word at a time is Not as good as when the text is buffered in the Speech object.
The way I see it (appreciate any feedback), is while both Speech and Word use Selection.Text
they (my conclusion) have separate buffers with no way to point to a common buffer.
Even if I load a sentence into the Speech buffer (which will speed up reading and allow easier tracking then loading the entire document), I still see the problem of correlating what word is being read (by Speech) with trying to highlight that word using the Word object model.
The Speech object may have a way to handle this (still delving into this), but will require a VB project instead of a quick an dirty app using Word.
Originally planned to release code to code bank once resolved in case others have sight accessiblity issues, but on second thought included below in hope some has more insight on Speech.
Code:
'Create Speech object
Private speech As SpVoice
Public gblnStopSpeak As Boolean
Sub SpeakBar()
'Toggle the Speak Toolbar in the Top Menu
With CommandBars("Speak")
.Visible = Not .Visible
End With
End Sub
Sub SpeakStart_New()
'Speak Word Text one word at a time
'beginning at cursor
On Error Resume Next
'*******
'STARTUP
'*******
'Start where we left off
Call BookmarkGoTo("SpeakStop")
gblnStopSpeak = False
'Set object
Set speech = New SpVoice
'*******
'MAIN
'*******
For Each vWord In ActiveDocument.Words
If gblnStopSpeak Then Exit For
'Flag the word to be read
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Range.Font.Underline = True
'Say the word
speech.Speak Selection.Text, SVSFlagsAsync + SVSFPurgeBeforeSpeak
'Slow down the loop
t = Timer
Do Until Timer - t > 0.4 'was 1 orig 2
DoEvents
Loop
'Unflag the just read word
Selection.Range.Font.Underline = False
Selection.MoveRight Unit:=wdWord, Count:=1
Next
'*******
'WRAPUP
'*******
Set speech = Nothing
End Sub
Sub SpeakStop_New()
'Set global variable to Exit Word Loop for speaking
gblnStopSpeak = True
'Reset the Bookmark to where we stopped
'Put call in this procedure so can
'easily reposition Bookmark by just selecting Stop
Call BookmarkSet("SpeakStop")
End Sub
Re: {Word} Highlight or Underline word while Speaking
Quote:
The Speech object may have a way to handle this (still delving into this), but will require a VB project instead of a quick an dirty app using Word.
i don't at this point see why
if there is provision in the speech code for any progress bar, you might be able to adapt that to the highlighting
Re: {Word} Highlight or Underline word while Speaking
I don't disagree.
Maybe its just the pronunciation of the words by the voice -- not clear as I think it should be.
Going to take a few days and delve more into SAPI.
Will post back to this thread in a few days.
Maybe someone else in the interim may have some thoughts.
Re: {Word} Highlight or Underline word while Speaking
OK M$ Word and Speech has got me confused.
Using the following simple SAPI example of text to speech in a VB5/6 textbox,
lets SAPI read the text and also highlights each word as it reads making use of the
"event _Word".
Code:
Option Explicit
'The following Visual Basic form code demonstrates
'the use of the Word event.
'To run this code, create a form with the following controls:
' * Reference Micrsoft Speech Object Library
' (This is installed with the OS, at least XP)
' * A command button called Command1
' * A text box called Text1
' * Set the HideSelection property of Text1 to False
'Paste this code into the Declarations section of the form.
'The Form_Load procedure puts a text string in Text1 and
'creates a voice object.
'The command1_Click procedure calls the Speak method.
'This will cause the TTS engine to send the Word event to the voice;
'the Word event code will use the event parameters to highlight
'the word associated with the event.
Public WithEvents vox As SpeechLib.SpVoice
Private Sub Command1_Click()
vox.Speak Text1.Text, SVSFlagsAsync
End Sub
Private Sub Form_Load()
Set vox = New SpVoice
Text1.Text = "This is some text in a textbox."
End Sub
Private Sub vox_Word(ByVal StreamNumber As Long, ByVal StreamPosition As Variant, _
ByVal CharacterPosition As Long, ByVal Length As Long)
' In order to show this selection,
' the Text1.HideSelection property must be False!
Text1.SelStart = CharacterPosition
Text1.SelLength = Length
End Sub
========================
On the other hand when I declare WithEvents in the Normal.dot template,
All the Speech events are available in Word VBA from
“Microsoft Word Object, ThisDocument” under
Normal.dot template, however when executing the following code,
with a line of Text in Document1, the line is spoken correctly, but
None of the SAPI events trigger.
Code:
Sub SpeakStart()
'Speak Word Text
' On Error Resume Next
Set vox = New SpVoice
'This selects the events which will be executed.
'If more than one specific event use "Or" between them
'Property sets OK, but Doesn't raise event????????
‘ vox.EventInterests = SVEAllEvents 'SVEWordBoundary
' MsgBox vox.EventInterests, vbInformation
If Len(Selection.Text) > 1 Then 'speak selection
vox.Speak Selection.Text, _
SVSFlagsAsync '+ SVSFPurgeBeforeSpeak
Else 'speak whole document
vox.Speak ActiveDocument.Range(0, ActiveDocument.Characters.Count).Text, _
SVSFlagsAsync '+ SVSFPurgeBeforeSpeak
End If
Do
DoEvents
Loop Until vox.WaitUntilDone(10)
Set vox = Nothing
End Sub
Even trying to specifically reference the SAPI event (never seen MS do this
-- allow user to refer to a specify which event(s) they want the queue to execute by setting a property) also does Not trigger any SAPI event.
Code:
vox.EventInterests = SVEAllEvents 'also just tried this event SVEWordBoundary
1 Attachment(s)
Re: {Word} Highlight or Underline word while Speaking
i am using office 2000 /xp sp2
when i test in word, declaring spvoice withevents i am getting no events listed
in excel i get all the events as expected, so either something funny is going on in word or my computer is just overworked
i then tested, in word, other objects withevents and am finding no events are being listed for those variables, i am about out of time for now, but will test, some more, later on other computer
Re: {Word} Highlight or Underline word while Speaking
At least that helps confirm the issue.
I'm using XP-Pro, SP3, with Word97.
And I saw you created a Class Module too.
Re: {Word} Highlight or Underline word while Speaking
i tested again, now, in word 2000 win7 limited user
worked correctly in either a class module or document object module
Quote:
And I saw you created a Class Module too.
withevents can not be done in a standard module
so i do not know why it was not working in word on xp, but would in excel,
if you do not get the drop down list (as in my pic) then the events are not going to work
Re: {Word} Highlight or Underline word while Speaking
Code:
withevents can not be done in a standard module
Aware of that, but thanks for the reminder.
Code:
so i do not know why it was not working in word on xp, but would in excel,
if you do not get the drop down list (as in my pic) then the events are not going to work
I got (get) the drop down list in Word when placed in Normal, “Microsoft Word Object.
Yet never triggers.
Going to try one last thing -- toggle a number of the word properties -- and see what happens.
Thanks for all your effort on my behalf.
David