Run next lines of code while voice still playing

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Code:
Sub TestVoices()
Dim i  As Long
Dim voc As SpeechLib.SpVoice
Set voc  = New SpVoice

i  = [RANDBETWEEN(0,2)]

Set voc.Voice = voc.GetVoices.Item(i)

voc.Speak "I want to run next lines while voice is playing"

MsgBox i 

End Sub

In other versions I could do that , like this:

Code:
Application. Speech.Speak "Hello", SpeakAsync:=True

What is it that I have to do to the first code to let the alert show while the voice is playing?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
:confused: What is preventing you from using ...

Code:
Application. Speech.Speak "I want to run next lines while voice is playing", SpeakAsync:=True

It works with Excel365
 
Last edited:
Upvote 0
:confused: What is preventing you from using ...

Code:
Application. Speech.Speak "I want to run next lines while voice is playing", SpeakAsync:=True

It works with Excel365

Because that only plays with the first voice . I want to switch among the three voices.
 
Upvote 0
Code:
Sub KellyMort()
    Dim V1 As String, V2 As String, V3 As String, i As Long
    V1 = "I want to run next lines while voice is playing"
    V2 = "I want to walk and talk"
    V3 = "I want to sleep for a week"
    i = [RANDBETWEEN(0,2)]
    Application.Speech.Speak Array(V1, V2, V3)(i), SpeakAsync:=True
    MsgBox i
End Sub


EDIT
I don't thin that what you want ...
Do you want to use 3 different voices, not 3 different "speeches"
 
Last edited:
Upvote 0
I have never use this speech library so I don't know if the following trick would work :

If you place the code (MsgBox) inside a one-off windows timer callback function then the code might run asynchroniously like you are wanting.


In a Standard Module:
Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Declare PtrSafe Function SetTimer Lib "user32" (ByVal hWnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
    Declare PtrSafe Function KillTimer Lib "user32" (ByVal hWnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If


Sub TestVoices()

    Dim i  As Long
    Dim voc As SpeechLib.SpVoice
    Set voc = New SpVoice
    
    i = [RANDBETWEEN(0,2)]
    
    [COLOR=#ff0000]Call SetTimer(Application.hWnd, [B]i[/B], 0, AddressOf NextLinesOfCode)[/COLOR]
    
    Set voc.Voice = voc.GetVoices.Item(i)

    voc.Speak "I want to run next lines while voice is playing"

End Sub


Sub NextLinesOfCode(ByVal hWnd As Long, ByVal uMsg As Long, ByVal TimerID As Long, ByVal Tick As Long)
    
    On Error Resume Next
    
    Call KillTimer(hWnd, TimerID)
    
    MsgBox TimerID [COLOR=#008000][B]'<==Shows the value returned by RANDBETWEEN[/B][/COLOR]
    
End Sub

What you can do in the callback is very limited for you can't interact with Properties and Methods of the excel object model.
 
Upvote 0
Code:
Sub KellyMort()
    Dim V1 As String, V2 As String, V3 As String, i As Long
    V1 = "I want to run next lines while voice is playing"
    V2 = "I want to walk and talk"
    V3 = "I want to sleep for a week"
    i = [RANDBETWEEN(0,2)]
    Application.Speech.Speak Array(V1, V2, V3)(i), SpeakAsync:=True
    MsgBox i
End Sub


EDIT
I don't thin that what you want ...
Do you want to use 3 different voices, not 3 different "speeches"

Three different voices
 
Upvote 0
@Jaafar Tribak,

It does not show the message alert while playing the voice. It only switches the voices. Shows the alert after the voice has finished playing.
 
Upvote 0
@Jaafar Tribak,

It does not show the message alert while playing the voice. It only switches the voices. Shows the alert after the voice has finished playing.

Ok.

Can you test this code that uses the Application.Speech Object with the SpeakAsync argument set to False and tell me if the MsgbOx shows While the voice is playing or After ?

Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Declare PtrSafe Function SetTimer Lib "user32" (ByVal hWnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
    Declare PtrSafe Function KillTimer Lib "user32" (ByVal hWnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If


Sub TestVoices()

    Dim i  As Long
    
    i = [RANDBETWEEN(0,2)]
    
    Call SetTimer(Application.hWnd, i, 0, AddressOf NextLinesOfCode)
    
    Application.Speech.Speak "I want to run next lines while voice is playing"

End Sub


Sub NextLinesOfCode(ByVal hWnd As Long, ByVal uMsg As Long, ByVal TimerID As Long, ByVal Tick As Long)
    
    On Error Resume Next
    
    Call KillTimer(hWnd, TimerID)
    
    MsgBox TimerID
    
End Sub
 
Upvote 0
@Jaafar Tribak,

It shows the MsgBox while playing voice but only plays with the first voice. Even when the alert shows 1 or 2 it plays the 0
 
Upvote 0
@Jaafar Tribak,

It shows the MsgBox while playing voice but only plays with the first voice. Even when the alert shows 1 or 2 it plays the 0

Not knowing about the SpeechLib library, I don't think I can help any furhter.

VBA can't do multithreading unless the code is executed inside a loaded dll.

One possible workaround is to run the voice from a seperate excel instance but that will rquire some setup work .
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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