Voice activation software to run macros?

TedX

Board Regular
Joined
Apr 18, 2021
Messages
122
Office Version
  1. 365
Platform
  1. Windows
One of the reasons I like creating macros to speed things along in Excel is that I'm lazy. I assume I am not alone in that department 🤪

So my question is, is there any voice activation software around (even in Beta) that would let me say commands that would run macros?
 
  • Voice turns an unlimited amount of buttons into one button.
    You could turn those six ⚠️ buttons into a single "region" button that you say to Excel what region to run.
I went all over Excel.XlRegionLabelOptions enumeration (Excel) and other areas that came up when I searched for Region, but couldn't see any relevant section. How exactly do you make my 6 state macros into a single region? I know what you're thinking, why did I ever reply to this dude 🥰
 
Upvote 0
I went all over Excel.XlRegionLabelOptions enumeration (Excel) and other areas that came up when I searched for Region, but couldn't see any relevant section. How exactly do you make my 6 state macros into a single region? I know what you're thinking, why did I ever reply to this dude 🥰

I don't mean an Excel inbuilt function.
I mean you would have your 6 macros but your voice recognition macro would have 6 options depending on what you say and you run the listening sub on your workbook open event / event of your choice / keyboard macro.

This is a very fast and loose psuedo code example with most of it from that Wise Owl tutorial. Don't expect it to just copy-paste in and work.
VBA Code:
Option Explicit
Private WithEvents ListeningSession As SpSharedRecoContext
Private Grammar As ISpeechRecoGrammar

Private Sub UserForm_Initialize()
    If (ListeningSession Is Nothing) Then
        Set ListeningSession = New SpSharedRecoContext
        Set Grammar = ListeningSession.CreateGrammar(1)
        Grammar.DictationLoad
    End If

    Grammar.DictationSetState (SpeechRuleState.SGDSActive)
End Sub

Private Function ListeningSession_Recognition( _
    ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
    ByVal Result As SpeechLib.ISpeechRecoResult)

    Return Result.PhraseInfo.GetText
End Sub

Sub Victoria()
End Sub

Sub NSW()
End Sub

'.....etc

Sub runThisWhenWorkbookOpens()
   Select Case ListeningSession_Recognition()
   Case "Run Victoria"
      Victoria()
   Case "Run New South Wales"
      NSW()
   Case "Run South Australia"
      SA()
   Case "Run Queens Land"
      QLD()
   Case "Run Western Australia"
      WA()
    Case "Run Japan"
      日本()
End Sub
 
Upvote 0
I just want to say "Run NSW), I would change my macro name to "run_nsw", so that the name and what I say are the same ..

I don't have a microphone to test this, but based on the link provided by Dan , you can adapt the ListeningSession_Recognition event listener routine to run the run_nsw macro as follows :

VBA Code:
Private Sub ListeningSession_Recognition( _
    ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
    ByVal Result As SpeechLib.ISpeechRecoResult)
   
    With Result.PhraseInfo
        If LCase(.GetText) = LCase("run_nsw") Then
            Call Application.Run(.GetText)
        End If
    End With

End Sub
 
Upvote 0
I missed the underscore _ character in my above code

If your voice says Run NSW and the corresponding macro is Run_NSW then , I think, the following code should work : ( Same logic applies to the other macros\states )

VBA Code:
Private Sub ListeningSession_Recognition( _
    ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
    ByVal Result As SpeechLib.ISpeechRecoResult)
   
    Dim sMacro As tring
   
    Select Case LCase(Result.PhraseInfo.GetText)
        Case "run nsw", "runnsw"
            sMacro = "run_nsw"
        Case "run vic", "runvic"
            sMacro = "run_vic"
        Case "run qld", "runqld"
            sMacro = "run_qld"
        ' Case othet states ...
    End Select
   
    If Len(sMacro) Then
        Call Application.Run(sMacro)
    End If

End Sub
 
Last edited:
Upvote 0
I missed the underscore _ character in my above code

If your voice says Run NSW and the corresponding macro is Run_NSW then , I think, the following code should work : ( Same logic applies to the other macros\states )

VBA Code:
Private Sub ListeningSession_Recognition( _
    ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
    ByVal Result As SpeechLib.ISpeechRecoResult)
  
    Dim sMacro As tring
  
    Select Case LCase(Result.PhraseInfo.GetText)
        Case "run nsw", "runnsw"
            sMacro = "run_nsw"
        Case "run vic", "runvic"
            sMacro = "run_vic"
        Case "run qld", "runqld"
            sMacro = "run_qld"
        ' Case othet states ...
    End Select
  
    If Len(sMacro) Then
        Call Application.Run(sMacro)
    End If

End Sub

I'm trying this solution first, I had to change to a different macro because those state ones will wipe out the current pages and start again, which is something I do at night, not during the day.
zzzzz481.jpg


The macro I'm trying simply hides a number of columns, it's called unhide_e_to_j and as you can see, I've typed it into the case section. I wasn't sure where to put it, so I tried This Workbook and got the error message shown in the above image.

So then I placed the code into a new module and spoke the words "unhide e to j" and got some text written into a cell on the open spreadsheet, it was the last cell I used which happened to be selected. So, looking on the bright side, the code when in its own module, does hear that I am speaking and then tries to write it into a cell. That is something, I just need it not to write anything and to make the macro work. If you think my macro names are too confusing and need to be simplified, so that I can just say one short word, then I can try that.
 
Upvote 0
You will need to place the code in a Class module in order to sink the ListeningSession_Recognition event. The class module could be a userform too.

Add a new class module to your vba project and name the module : CVoiceListener


1- Class Module code:
VBA Code:
Option Explicit

Private WithEvents ListeningSession As SpSharedRecoContext
Private Grammar As ISpeechRecoGrammar

Private Sub Class_Initialize()
    If (ListeningSession Is Nothing) Then
        Set ListeningSession = New SpSharedRecoContext
        Set Grammar = ListeningSession.CreateGrammar(1)
        Grammar.DictationLoad
    End If
    Grammar.DictationSetState (SpeechRuleState.SGDSActive)
End Sub

Private Sub ListeningSession_Recognition( _
    ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
    ByVal Result As SpeechLib.ISpeechRecoResult)
 
    Dim sMacro As String
 
    Select Case LCase(Result.PhraseInfo.GetText)
        Case "run nsw", "runnsw"
            sMacro = "run_nsw"
        Case "run vic", "runvic"
            sMacro = "run_vic"
        Case "run qld", "runqld"
            sMacro = "run_qld"
        ' Case other states ...
    End Select
 
    If Len(sMacro) Then
        Call Application.Run(sMacro)
    End If

End Sub


2- Place the following code In a Standard Module: (Run the InitVoiceListener routine to initiate the voice recognition)
VBA Code:
Option Explicit

Private oVoice As CVoiceListener

Sub InitVoiceListener()
    Set oVoice = New CVoiceListener
End Sub

Sub run_nsw()
    MsgBox "You ran: " & vbLf & "run_nsw"
End Sub

Sub run_vic()
    MsgBox "You ran: " & vbLf & "run_vic"
End Sub

Sub run_qld()
    MsgBox "You ran: " & vbLf & "run_qld"
End Sub


The above assumes the following:

1- You have already set a reference to the Microsoft Speech Object Library via Tools\References.
2- The names of the macros you want to run with your voice contain an uderscore in the middle.
3- You call the macros saying : Run NSW , Run VIC, Run QLD ... etc
 
Last edited:
Upvote 0
Jaafar's method tested and works, alas microsoft speech recognition is really hard to talk to (or I speak really bad XD).
 
Upvote 0
You have already set a reference to the Microsoft Speech Library via Tools\References.
1670564524465.png


I have this one and I noticed when I scrolled down further that there was this one:

1670564680981.png


But I can't have them both because I get the following error:

1670564743937.png


I have not actually tried your solution yet. I had to go and look up what a Class Module was:
Something else I have learned today from this marvellous forum 😍
I will try this when I can later tonight, I swear I will definitely try to put it all together. I'm keeping all my eyes firmly glazed over

1670564933299.png
 
Upvote 0
Use the library that on the second screenshot . That's the one which references the sapi.dll file.
 
Upvote 0

Forum statistics

Threads
1,226,848
Messages
6,193,317
Members
453,790
Latest member
yassinosnoo1

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