Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Application.Speech.Speak read some numbers individually rather than put them together?

Tags:

excel

vba

speech

Let's suppose now it is 11h11min. It reads "ONE ONE" hours and "eleven" minutes, as in:

Sub TEST1() 
  Application.Speech.Speak "It is " & Hour(Now()) & " hours and " & Minute(Now()) & " minutes"
End Sub

However, the following reads "eleven" hours and "eleven" minutes

Sub TEST2() 
  Application.Speech.Speak "It is 11 hours and 11 minutes"
End Sub

On the contrary, it reads "ONE ONE" hours and "eleven" minutes, as in:

Sub TEST3() 
  Application.Speech.Speak "It is " & "11" & " hours and " & "11" & " minutes"
End Sub

How can I get it to read these numbers as words?

like image 331
Luiz Vaughan Avatar asked Jan 29 '23 16:01

Luiz Vaughan


2 Answers

You need to create/modify the string to produce the results you want.

Sub dural()
    Application.Speech.Speak "11"
    Application.Speech.Speak "eleven"
End Sub

will repeat the same thing.

Sub dural()
    Application.Speech.Speak "1 1"
    Application.Speech.Speak "one one"
End Sub

will do the same. Once you have decided what you want the computer to say, you can format/re-format the string to produce that result.

EDIT#1:

On my computer this:

Sub dmral()
    Dim s As String
    s = "It is " & "11" & " hours and " & "11" & " minutes"
    MsgBox s
    Application.Speech.Speak s
End Sub

also works as expected.

It looks like the concatenation needs to be performed first.

like image 133
Gary's Student Avatar answered Jan 31 '23 11:01

Gary's Student


Approach via SpeakXML argument

Syntax

.Speak(Text, SpeakAsync, SpeakXML, Purge)

If you set the 3rd argument SpeakXML to True, you can use XML tags in your text string.

The XML <spell> tag forces the voice to spell out all text, rather than using its default word and sentence breaking rules. All characters should be expanded to corresponding words (including punctuation, numbers, and so forth). Note that the <spell> tag mustn't be empty and don't forget the closing tag </spell>.

Try to use the following with both variants of 11:

Code

Sub TEST()
  Application.Speech.Speak "It is " & "<spell>11</spell>" & " hours and " & "11" & " minutes", False, SpeakXML:=True
End Sub

Note/caveat

I'm using a central European language Version and your example did'nt speak out ONE ONE in my case, so maybe there is another local setting issue.

like image 34
T.M. Avatar answered Jan 31 '23 10:01

T.M.