VBA - Application.Speech.Speak TroubleShoot

Cubist

Well-known Member
Joined
Oct 5, 2023
Messages
1,907
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I'm testing out the Application.Speech.Speak.This is new for me.
However, when I ran the code, the application said that it was [running] but I didn't hear anything. When I pause the code, I get the "Application-defined or object-defined error". Do I need to set up something first?

This is the simple code.

VBA Code:
Sub UseSpeech()
 
 Application.Speech.Speak "Hello"
 
End Sub

My volume is up to the max. When I do Preview Voice in the Speech Settings. I can hear "David" speaks.
1714161850341.png
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Addendum: I've also added the "Microsoft Speech Object" library.
 
Upvote 0
My EnableEvents was FALSE. Got it to work.
Wrote a greeting VBA and tell a random joke.
VBA Code:
Sub GreetingsAndTellJoke()

 uName = Split(Application.UserName)(0)
 Select Case Hour(Now())
    Case 0 To 11
        timeOfday = "morning"
    Case 12 To 17
        timeOfday = "afternoon"
    Case Else
        timeOfday = "evening"
 End Select

 Application.Speech.Speak "Good " & timeOfday & " " & uName & ". Here's your joke for the day.", True
 TellMeAJoke
End Sub
Sub TellMeAJoke()
    Dim http As Object
    Set http = CreateObject("MSXML2.XMLHTTP")
    ' Make a GET request to the icanhazdadjoke API
    http.Open "GET", "https://icanhazdadjoke.com/", False
    http.setRequestHeader "Accept", "application/json"
    http.send
    ' Parse the response JSON to extract the joke
    Dim jokeResponse As String
    jokeResponse = http.responseText
    ' Extract the joke from the response JSON
    Dim joke As String
    joke = ExtractJokeFromResponse(jokeResponse)
    ' Display the joke
     Application.Speech.Speak joke
End Sub
 
Function ExtractJokeFromResponse(response As String) As String
    Dim startIndex As Long
    Dim endIndex As Long
    startIndex = InStr(response, """joke"":""") + Len("""joke"":""")
    endIndex = InStr(startIndex, response, """")
    ExtractJokeFromResponse = Mid(response, startIndex, endIndex - startIndex)
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top