Pages in topic:   [1 2] >
Visual Basic Macro: Can someone debug it?
Thread poster: Patricia Rosas
Patricia Rosas
Patricia Rosas  Identity Verified
United States
Local time: 07:47
Spanish to English
+ ...
In memoriam
Jul 30, 2009

The other day, I asked for help to get the text-to-speech feature running in Word 2007. Andy Tolle gave me a macro, that I've found out on the Internet at this address, too:
http://www.gmayor.com/word_text_to_speech.htm
(the macro itself appears toward the bottom of the page).

Everything works great for the "Speak Text" button, but I cannot get the "Stop Speaking"
... See more
The other day, I asked for help to get the text-to-speech feature running in Word 2007. Andy Tolle gave me a macro, that I've found out on the Internet at this address, too:
http://www.gmayor.com/word_text_to_speech.htm
(the macro itself appears toward the bottom of the page).

Everything works great for the "Speak Text" button, but I cannot get the "Stop Speaking" button to work. I've never worked with VB, and I'm wondering if there is an error in this part of the macro. I'm hoping that someone familiar with VB might be able to spot it (or tell me how to get the debugger to work, because I'm not getting error messages):

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

Any light that can be shed on this problem will be greatly appreciated!
Patricia
Collapse


 
Daniel Grau
Daniel Grau  Identity Verified
Argentina
Member (2008)
English to Spanish
Define "speech" as global? Jul 30, 2009

In this other script, they define "speech" outside the subroutines, whereby it becomes global:

http://snippets.dzone.com/tag/VBScript

Regards,

Daniel


 
tectranslate ITS GmbH
tectranslate ITS GmbH
Local time: 16:47
German
+ ...
speech is probably undefined Jul 30, 2009

As Daniel already said, "speech" is probably undefined in this context.

To get the debugger to work, you need to disable the line "On Error Resume Next".

HTH,

Benjamin


 
Patricia Rosas
Patricia Rosas  Identity Verified
United States
Local time: 07:47
Spanish to English
+ ...
TOPIC STARTER
In memoriam
where or how? Jul 30, 2009

Daniel,

I looked at the code for "taking a break" but I'm so new to this that I don't understand what you mean by "defining speech outside the subroutines." The full routine that I'm running is this (and I think the very first line is what you are talking about, right?):

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
... See more
Daniel,

I looked at the code for "taking a break" but I'm so new to this that I don't understand what you mean by "defining speech outside the subroutines." The full routine that I'm running is this (and I think the very first line is what you are talking about, right?):

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

Thanks,
Patricia
Collapse


 
Terry Richards
Terry Richards
France
Local time: 16:47
French to English
+ ...
I think... Jul 30, 2009

Before I start, I'm not that familiar with VB and I know nothing about the SpVoice object. But, I have been programming professionally for over 30 years...

It looks to me like your text has got split up (probably so it fits into tye line length of a book?). The two lines ending in an underscore should be unwrapped (delete the underscore and the new line so the following line flows up). If these lines are being treated as separate statements, the async parameter is not being passed t
... See more
Before I start, I'm not that familiar with VB and I know nothing about the SpVoice object. But, I have been programming professionally for over 30 years...

It looks to me like your text has got split up (probably so it fits into tye line length of a book?). The two lines ending in an underscore should be unwrapped (delete the underscore and the new line so the following line flows up). If these lines are being treated as separate statements, the async parameter is not being passed to the SpVoice object which (I guess) means that it is running synchronously - i.e. it is not yielding to the message queue and your second routine isn't getting triggered until the first one has finished.

If this is the case, deleting the underscores and line ends will fix it. If not, deleting them will do no harm.

You could also put a message box at the beginning of StopSpeaking to see if it is getting called at all and, if so, when it is getting called. Does the message appear while it is still speaking or does it appear after speaking has finished or does it not appear at all?

Terry.
Collapse


 
tectranslate ITS GmbH
tectranslate ITS GmbH
Local time: 16:47
German
+ ...
Helpful? Jul 30, 2009

A search for "as spvoice" gives several interesting results, one of which leads to this illustrated guide:
http://www.gmayor.com/word_text_to_speech.htm

HTH,

Benjamin

P.S.: The underscore is what allows the wrapped code to still work (by VB conventions). But yes, you can delete it and put the contents of the 2nd line on the first line and it'll wo
... See more
A search for "as spvoice" gives several interesting results, one of which leads to this illustrated guide:
http://www.gmayor.com/word_text_to_speech.htm

HTH,

Benjamin

P.S.: The underscore is what allows the wrapped code to still work (by VB conventions). But yes, you can delete it and put the contents of the 2nd line on the first line and it'll work just as well.

[Edited at 2009-07-30 19:12 GMT]
Collapse


 
Jaroslaw Michalak
Jaroslaw Michalak  Identity Verified
Poland
Local time: 16:47
Member (2004)
English to Polish
SITE LOCALIZER
Declarations Jul 30, 2009

Benjamin is right, underscores allow to split the lines in the source, so it is not a problem...

The issue, I think, is that the line:

Dim speech as SpVoice 'Don't overlook this line!

has to be at the very start of the module, not the procedure... Module is the whole text block you see in the editor, most probably it is called "NewMacros" (you can see all the modules in the tree on the left).

If you have any other user macros, it might b
... See more
Benjamin is right, underscores allow to split the lines in the source, so it is not a problem...

The issue, I think, is that the line:

Dim speech as SpVoice 'Don't overlook this line!

has to be at the very start of the module, not the procedure... Module is the whole text block you see in the editor, most probably it is called "NewMacros" (you can see all the modules in the tree on the left).

If you have any other user macros, it might be that the line got attached to them, like this:

Code:

Sub Someothermacro()

...

End sub

Dim speech as SpVoice 'Don't overlook this line!

Sub SpeakText()



What you have to do is to move the line to the very start of the whole module. You will know if it is correct, when the cursor placed on the line will change the text of the combo boxes at the top from (General) SomeOtherMacro to (General)(Declarations).

Of course, you can also put the macros into another module, but this is slightly more complicated...
Collapse


 
Terry Richards
Terry Richards
France
Local time: 16:47
French to English
+ ...
Continuation character Jul 31, 2009

So the underscore is a VB continuation character. I didn't know that. In that case, the first part of my answer is nonsense

However, the bit about the message box is still useful. It will tell you if the subroutine is being called at all and, if it is, when it is being called.

T.


 
Patricia Rosas
Patricia Rosas  Identity Verified
United States
Local time: 07:47
Spanish to English
+ ...
TOPIC STARTER
In memoriam
that was the page I copied Jul 31, 2009

tectranslate wrote:

A search for "as spvoice" gives several interesting results, one of which leads to this illustrated guide:
http://www.gmayor.com/word_text_to_speech.htm

HTH,

Benjamin



Actually, this link is the one I copied the code from. But thanks for sharing it.


 
Patricia Rosas
Patricia Rosas  Identity Verified
United States
Local time: 07:47
Spanish to English
+ ...
TOPIC STARTER
In memoriam
I'm trying to follow ... Jul 31, 2009

Jabberwock wrote:

What you have to do is to move the line to the very start of the whole module. You will know if it is correct, when the cursor placed on the line will change the text of the combo boxes at the top from (General) SomeOtherMacro to (General)(Declarations).

Of course, you can also put the macros into another module, but this is slightly more complicated...


Sorry, I'm being such a dunce, but as far as I can tell, the line is at the very beginning:

Sub TextToSpeech()

Dim speech As SpVoice

End Sub
***HERE A GREY LINE APPEARS ACROSS THE SCREEN***
Sub SpeakText()
'Based on a macro by Mathew Heikkila


But when I place the cursor on the first line, the combo boxes read "General" and it switches to "TextToSpeech," the name of the macro. It should read "Declarations"? Do you think that somehow the Sub TextToSpeech() isn't really at the top, even though it appears to be?



Thanks again, everyone!


 
Patricia Rosas
Patricia Rosas  Identity Verified
United States
Local time: 07:47
Spanish to English
+ ...
TOPIC STARTER
In memoriam
tried again and still no luck Jul 31, 2009

I realized that there was a blank line before the Sub TextToSpeech line, so I removed it, and when I put the cursor on the phrase, the box read "Declarations." I saved everything, removed the old macro buttons from the ribbon, and reinstalled the new version.

But the Stop Speaking subroutine still doesn't work.

When I returned to VB, the module had changed and the first line was highlighted in yellow with an arrow, and the box again said "TextToSpeech"...

... See more
I realized that there was a blank line before the Sub TextToSpeech line, so I removed it, and when I put the cursor on the phrase, the box read "Declarations." I saved everything, removed the old macro buttons from the ribbon, and reinstalled the new version.

But the Stop Speaking subroutine still doesn't work.

When I returned to VB, the module had changed and the first line was highlighted in yellow with an arrow, and the box again said "TextToSpeech"...

Any other ideas??
Collapse


 
Jaroslaw Michalak
Jaroslaw Michalak  Identity Verified
Poland
Local time: 16:47
Member (2004)
English to Polish
SITE LOCALIZER
Before the Sub... Jul 31, 2009

The Dim line is supposed to be the first line of the module, i.e. it should come before the very first Sub command.

Try removing the TexttoSpeech() macro (it is not supposed to be there anyway)... Move the cursor all the way up, put the Dim line (it should indicate Declarations) there and only then you can have the first Sub...

I hope this is clear enough... If you do not have any other macros defined (and from your description it seems that you don't), ju
... See more
The Dim line is supposed to be the first line of the module, i.e. it should come before the very first Sub command.

Try removing the TexttoSpeech() macro (it is not supposed to be there anyway)... Move the cursor all the way up, put the Dim line (it should indicate Declarations) there and only then you can have the first Sub...

I hope this is clear enough... If you do not have any other macros defined (and from your description it seems that you don't), just open the Editor, delete everything from the window and paste the code exactly as it appears on the linked page (i.e. starting with the Dim line).



[Edited at 2009-07-31 19:25 GMT]
Collapse


 
Patricia Rosas
Patricia Rosas  Identity Verified
United States
Local time: 07:47
Spanish to English
+ ...
TOPIC STARTER
In memoriam
screen shot below . . . Jul 31, 2009

I did what you suggested, and stop speaking macro still won't work; here's a screen shot from the module:

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
... See more
I did what you suggested, and stop speaking macro still won't work; here's a screen shot from the module:

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



Between each End Sub and Sub and after the Dim Speech line, there is a pale grey line. I save everything in Normal, dump the old icons, reinsert the new ones on the ribbon, but it still doesn't work. Also, the cursor on the Dim line makes the box read declarations ...
Collapse


 
Jaroslaw Michalak
Jaroslaw Michalak  Identity Verified
Poland
Local time: 16:47
Member (2004)
English to Polish
SITE LOCALIZER
Hmm... Jul 31, 2009

In that case I do not know why it fails, it works in my case...

You should comment out (put the apostrophe) the line

On Error Resume Next

in the StopSpeaking procedure - then you will know what is the error code...

Alternately, you may press Ctrl+Pause/Break key, this will stop the macro execution, but is less convenient, of course.


 
Patricia Rosas
Patricia Rosas  Identity Verified
United States
Local time: 07:47
Spanish to English
+ ...
TOPIC STARTER
In memoriam
where is the pause key? Jul 31, 2009

Dear Jabberwock,

I've been deeply grateful for your help. I tried the commenting out step, but the text did not turn green. Does that matter?

Can you tell me where the pause/break key is on a keyboard?

Thanks again,
Patricia


 
Pages in topic:   [1 2] >


To report site rules violations or get help, contact a site moderator:


You can also contact site staff by submitting a support request »

Visual Basic Macro: Can someone debug it?






Trados Studio 2022 Freelance
The leading translation software used by over 270,000 translators.

Designed with your feedback in mind, Trados Studio 2022 delivers an unrivalled, powerful desktop and cloud solution, empowering you to work in the most efficient and cost-effective way.

More info »
Wordfast Pro
Translation Memory Software for Any Platform

Exclusive discount for ProZ.com users! Save over 13% when purchasing Wordfast Pro through ProZ.com. Wordfast is the world's #1 provider of platform-independent Translation Memory software. Consistently ranked the most user-friendly and highest value

Buy now! »