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