Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Run next lines of code while voice still playing
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Run next lines of code while voice still playing

    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?
    There Is Always A Better Way

  2. #2
    Board Regular
    Join Date
    Mar 2015
    Posts
    3,912
    Post Thanks / Like
    Mentioned
    72 Post(s)
    Tagged
    7 Thread(s)

    Default Re: Run next lines of code while voice still playing

    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 by Yongle; Sep 12th, 2019 at 03:13 AM.

  3. #3
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Run next lines of code while voice still playing

    Quote Originally Posted by Yongle View Post
    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.
    There Is Always A Better Way

  4. #4
    Board Regular
    Join Date
    Mar 2015
    Posts
    3,912
    Post Thanks / Like
    Mentioned
    72 Post(s)
    Tagged
    7 Thread(s)

    Default Re: Run next lines of code while voice still playing

    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 by Yongle; Sep 12th, 2019 at 04:17 AM.

  5. #5
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,351
    Post Thanks / Like
    Mentioned
    39 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Run next lines of code while voice still playing

    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
    
    #If  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
    #Else 
        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
    #End  If
    
    
    Sub TestVoices()
    
        Dim i  As Long
        Dim voc As SpeechLib.SpVoice
        Set voc = New SpVoice
        
        i = [RANDBETWEEN(0,2)]
        
        Call SetTimer(Application.hWnd, i, 0, AddressOf NextLinesOfCode)
        
        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 '<==Shows the value returned by RANDBETWEEN
        
    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.
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

  6. #6
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Run next lines of code while voice still playing

    Quote Originally Posted by Yongle View Post
    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
    There Is Always A Better Way

  7. #7
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Run next lines of code while voice still playing

    @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.
    There Is Always A Better Way

  8. #8
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,351
    Post Thanks / Like
    Mentioned
    39 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Run next lines of code while voice still playing

    Quote Originally Posted by kelly mort View Post
    @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
    
    #If  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
    #Else 
        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
    #End  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
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

  9. #9
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Run next lines of code while voice still playing

    @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
    There Is Always A Better Way

  10. #10
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,351
    Post Thanks / Like
    Mentioned
    39 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Run next lines of code while voice still playing

    Quote Originally Posted by kelly mort View Post
    @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 .
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •