Run next lines of code while voice still playing

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,707
Office Version
2016
Platform
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?
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,961
Office Version
365
Platform
Windows
: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:

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,707
Office Version
2016
Platform
Windows
: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.
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,961
Office Version
365
Platform
Windows
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:

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,561
Office Version
2016
Platform
Windows
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.
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,707
Office Version
2016
Platform
Windows
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
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,707
Office Version
2016
Platform
Windows
@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.
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,561
Office Version
2016
Platform
Windows
@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
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,707
Office Version
2016
Platform
Windows
@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
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,561
Office Version
2016
Platform
Windows
@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 .
 

Forum statistics

Threads
1,085,834
Messages
5,386,226
Members
401,986
Latest member
crt54

Some videos you may like

This Week's Hot Topics

Top