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?
 
Use the library that on the second screenshot . That's the one which references the sapi.dll file.
I got that fixed now, thank you Jaafar. I have two modules as per the image below, however the one on the left is supposed to be the Class Module but I haven't done anything to make it a Class module and I get the red line of text near the top. There are 2 modules, if I can just get the one on the left, Module 148 to become a Class module, I suspect that the red text will turn black. I think I need to do something like Dim abc As New ABasicClass but I'm not really sure. I just renamed Module 148 to CVoiceListener

1670566280330.png
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Okay, now when I'm on a worksheet, and I click on say a row number, I get this error, and my microphone is sleeping 💤💤💤
1670567297428.png
 
Upvote 0
1- Add the new Class Module:
1a.png



2- Click on the Properties Window:
2a.png



3- Change the default class module name from Class1 to CVoiceListener
3a.png



Finally, just paste the class code (post #17) in the class module code pane and save.
 
Upvote 0
I haven't had a chance to review the entire thread, or Jaafar's workbook, but I'm really looking forward to it.

The one note of caution I would add, having tried doing this in the past for someone, is that you should probably temper your expectations as to how well the speech recognition works. Also, I'd strongly recommend spending a bit of time with the WiseOwl demo workbook, and trying to say out loud the names of movies you want it to lookup, because you'll start to see that the speech-to-text functionality has certain idiosyncracies you need to be alive to if you're wanting to make it work. I started to go down a bit of a rabbit hole, giving up on expecting it to understand the spoken word, instead trying to give each written and spoken command a SoundEx equivalent and devising some kind of fuzzy matching.

Also, as foreshadowed in the code by @MCLIFTO8, I don't know that your spoken "NSW" is going to remotely resemble what the speech recognition is expecting. As per MCLIFTO8's code, I expect that the spoken command will need to be "Run New South Wales" because NSW will otherwise be something akind to "En Es DoubleYou", and I just don't know that the speech recognition system has that kind of power... I vaguely recall that the system did it's best recognition with single words, or maybe I'm getting that mixed up. I remember that @Gokhan Aycan was giving me some guidance last time.

Mind you, it's been a while since I looked at it, and I certainly didn't have the benefit of Jaafar's code to help me. Looking forward to learning more about it again.
 
Upvote 0
The one note of caution I would add...

Thank you very much for your advice, I bet you are correct, I suspected as much days ago about my macro naming conventions being able to be used as expressions, the speech would actually recognize. Believe it or not, I actually have a real job that annoys me to distraction and unbelievably they actually wanted me to do some real work, which has slowed me down. I will get this going because too many people have put too much effort into me and I won't let you down. It will take a few hours/days however and of course, that **** soccer world cup is also getting in the way 😂🤣😂
 
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

Hello again, I've created a simple macro called one, so all I should have to do is say 'one' and you would think that it would be able to recognize that single word relatively easy. After making the macro, I made some slight changes, as you can see in the image below, to correspond with the case and MsgBox. Unfortunately when I ran the InitVoiceListener() I got the error message. I just made the macro in the workbook I am using and ran the macro to make sure it's woring. Any ideas why I got that error message and at the same time, can you closely look at all three modules, the Class module on the left and two other standard modules. Is anything showing up that's shopuldn't be there. There are no macros named run_new etc. perhaps that is screwing it up, I thought if I could switch on the listener, andonly said 'one' that only that part of the code would run, do you think I should clean out the parts of the code that don't yet exist, like those cases for the other states. I really do have names for all the macros but they are long and stupid and if I can just get 'one' to work, I'll rename them all, as in 'two', 'three' and so on. Anyway, can you spot the reason for my error?

1670592329438.png
 
Upvote 0
Okay, new information: I cleared out the superfluous code that pointed to other macros and ran the sub InitVoiceListener() which appeared to work, in so far as I ran it and nothing happened, so I assume it was listening. Then I spoke one word, it was 'one' and that's when the error appeared. That means it was listening because the error only appeared after I spoke, after it heard me speak. I don't know if this is helping anyone, but I'll bet the city of Tucson to a Cuban cigar that I am only one step away from making this work 🙃😉

1670593302826.png
 
Upvote 0
You can check to see whether or not a certain part of the code has been executed by using Debug.Print statements. I find it's helpful to check to make sure all necessary parts of the programs flow have been executed. So you may want to put
VBA Code:
Debug.Print "Listening Session - Recognition - Initialised"
or something like that at the start of the relevant subroutine.

Also, just quickly, you don't have subroutines that share the same name, do you? I note that you have a Sub One in two separate modules. You want to avoid do that as much as possible, otherwise VBA will start complaining about ambiguity something something. If I'm saying something out-of-turn, or it's something you already know and I've just failed to pick it up on my quick skim read, my apologies....
 
Upvote 0
You can check to see whether or not a certain part of the code has been executed by using Debug.Print statements. I find it's helpful to check to make sure all necessary parts of the programs flow have been executed. So you may want to put
VBA Code:
Debug.Print "Listening Session - Recognition - Initialised"
or something like that at the start of the relevant subroutine.

Also, just quickly, you don't have subroutines that share the same name, do you? I note that you have a Sub One in two separate modules. You want to avoid do that as much as possible, otherwise VBA will start complaining about ambiguity something something. If I'm saying something out-of-turn, or it's something you already know and I've just failed to pick it up on my quick skim read, my apologies....
Ah Ha, you gave me a lead regarding debugging, so here is where the error actually occurs:

1670595409011.png
 
Upvote 0

Forum statistics

Threads
1,224,945
Messages
6,181,937
Members
453,074
Latest member
JefersonKollet

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