Answered by:
Changing the voice used in application.speech.speak inside a macro

Question
-
I am playing with a macro that uses application.speech.speak
In simple terms I'd like to be able to do something like:
Set the voice to Microsoft David Desktop
Application.Speech.Speak ("Good morning")
Set the voice to Microsoft Hazel Desktop
Application.Speech.Speak ("And a good morning to you. How are you?)
I am struggling to create the code to "Set the voice to ___"
I'd be delighted if somebody could help me with this.
Many thanks
Aarnout
Thursday, October 2, 2014 5:02 PM
Answers
-
hmm....
try this:
Sub Main() Dim speech As New SpVoice Dim i As Long Debug.Print speech.GetVoices.Count For i = 0 To speech.GetVoices.Count Set speech.voice = speech.GetVoices.Item(i) speech.Speak ("Hello World!") Application.Wait (Now() + TimeValue("00:00:05")) Next End Sub
- Marked as answer by Fei XueMicrosoft employee Friday, October 10, 2014 1:39 AM
Tuesday, October 7, 2014 8:02 AM -
Aarnout,
You are getting the error because you have not attached the library to the VBA project.
in Visual Basic Editor find Tools on the toolbar and then References. Scroll down the list and tick the box next to Microsoft Speech Object Library.
- Marked as answer by Fei XueMicrosoft employee Friday, October 10, 2014 1:39 AM
Monday, October 6, 2014 7:23 AM
All replies
-
Would this help at all?
http://support.microsoft.com/kb/289845
In VBA you would probably want to add references to Microsoft Speech Object Library via Tools -> References and tick the box
You can then quickly initialize the object of the type and see if there any any more voices available, for example
Dim speech As New SpVoice MsgBox "You have " & speech.GetVoices.Count & " voices installed"
and if you do have more than 1 voice you can use the following to change the current voice
Set speech.Voice = speech.GetVoices.Item(0)
replacing the 0 with a number between 0 and GetVoices.Count (Count)
- Edited by Michal Krzych Friday, October 3, 2014 7:53 AM
Friday, October 3, 2014 7:41 AM -
Dear Michal,
Many thanks for your kind effort to help me.
I had found the article on changing the voice via the control panel and doing that I know that my computer has three voices available.
My struggle is getting the VBA macro to do the switching.
When I add "Dim speech As New SpVoice" to the macro I get an error message: "User defined type not defined".
If you have any other suggestion I'd be delighted.
Thank you again for trying to help.
Best, Aarnout
- Edited by Aarnout Sunday, October 5, 2014 4:51 AM
Sunday, October 5, 2014 4:51 AM -
Aarnout,
You are getting the error because you have not attached the library to the VBA project.
in Visual Basic Editor find Tools on the toolbar and then References. Scroll down the list and tick the box next to Microsoft Speech Object Library.
- Marked as answer by Fei XueMicrosoft employee Friday, October 10, 2014 1:39 AM
Monday, October 6, 2014 7:23 AM -
Dear Michal,
Thank you for you help and patience with me. Please feel free to give up on me...
The code you provided is working in as much as it does not crash anymore now that I have attached the Library - thank you.
The message box tells me that I have three voices installed.
Sadly...
Set speech.Voice = speech.GetVoices.Item(0)
Application.speech.Speak ("Test")Set speech.Voice = speech.GetVoices.Item(2)
Application.speech.Speak ("Test")Does not chance the sound of the voice.
Sorry to be such a pain -- but if you can help me on this last hurdle, I'd really appreciate it.
Best wishes
Aarnout
Monday, October 6, 2014 3:02 PM -
Do you mind setting the default voice via the Control Panel / Text To Speech and running a simple verification code:
Application.Speech.Speak ("Good morning")
just to verify that all 3 voices are actually different?
Monday, October 6, 2014 4:11 PM -
Ah...That is interesting. When I change then in the control panel and preview the voices there they are very different. But running the verification code, the voice does not change...
I do hope that I have not been wasting your time.
Thanks
Aarnout
Monday, October 6, 2014 4:26 PM -
hmm....
try this:
Sub Main() Dim speech As New SpVoice Dim i As Long Debug.Print speech.GetVoices.Count For i = 0 To speech.GetVoices.Count Set speech.voice = speech.GetVoices.Item(i) speech.Speak ("Hello World!") Application.Wait (Now() + TimeValue("00:00:05")) Next End Sub
- Marked as answer by Fei XueMicrosoft employee Friday, October 10, 2014 1:39 AM
Tuesday, October 7, 2014 8:02 AM -
Dear Michal,
Totally awesome! It worked! Thank you so much.
Do you think I will need to always include the line: "Debug.Print speech.GetVoices.Count" and either way what did this line do?
Thank you for all the perseverance.
Best wishes
Aarnout
Tuesday, October 7, 2014 10:57 AM -
You can safely remove this line: Debug.Print speech.GetVoices.Count
It only prints out the number of voices available for the speech object to the Immediate Window.
Well, I am glad it has worked in the end, can you accept and upvote the answers :)
Tuesday, October 7, 2014 3:51 PM -
Sub read()
Dim speech As New SpVoice
Dim Line As String
Line = Sheets("Links").Cells(5, 1)
Application.speech.Speak "Hello" <<<<<<<< This is the first (default) voice
speech.Speak Line <<<<<<<< Still the default voice
Application.speech.Speak Range("A5") <<<<<<< Still the default
Application.speech.Speak Line <<<<<<< Still the default
MsgBox "You have " & speech.GetVoices.Count & " voices installed" <<<<<<< Only have 2 installed
Set speech.Voice = speech.GetVoices.Item(1) <<<<<<< Put 1 instead of 0 to change from default
speech.Speak Line <<<<<<< Changes to second voice
Application.speech.Speak Range("A5") <<<<<<< This is still default (first voice)
Speech.Speak Line <<<<<<< Changes to HER (second voice)
End SubThank you for the INFO!!!
Friday, December 20, 2019 7:23 PM -
Interesting ...
But how can I integrate in this Workaround the "SpeakSync" Parameter ?
Thanks for ideasSunday, January 3, 2021 8:15 AM