Part 1 — VBA Speech Recognition — SAPI STT
Simple shared instance — form or class
I am writing this for those — like me — starting out attempting to use Microsoft SR (sapi.dll) in a VBA context. I found numerous examples written for VB, C# and delphi and it was possible to piece this together using those resources but to save you the effort read this instead. I will be using Excel 2003 but the same principles should apply to its other Microsoft Office siblings and newer versions of Excel too.
For brevity I will assume you are reasonably familiar with VBA and its implementation in an Office application.
Key Steps
- Select the reference to the sapi.dll. This is probably available by default but I believe can be downloaded if not.
- Place the event code in either a class module or a form. Don’t worry I’ll explain what this means later.
- Create a trigger to start speech recognition. This can be a button or an event e.g. a form opening or a sheet being activated.
- Do something with the recognised text.
In later articles I’ll explain how to switch — and why — from a shared instance to an inproc one (actually quite easy) and how to limit the recognition to just a few words (also easy) so you can have a bespoke SR control mechanism for a form/ sheet or application.
Select the reference to the sapi.dll
Find and select the Microsoft Speech Object Library — which is sapi.dll.
Place the event code and triggers
This is probably the most useful bit as it probably the least clear part when looking at VB or other language implementations.
Class module version
Here the event code is created in a class module and the SR is activated by creating an object instance.
Create a class module — call it anything you like — I have named it clsSpeech.
At the top add the following two lines
Dim WithEvents RecoContext As SpSharedRecoContext
Dim grammar As ISpeechRecoGrammar
Due to the first line you will now be able to select RecoContext from the left dropdown menu and will see all its related events. Select “Recognition” and paste the following code inside the stub created.
Dim strText As String
strText = Result.PhraseInfo.GetText
Debug.Print strText
Again using the dropdowns at the top — select “Class” from the left and “Initialise” from the right and paste the following code in the stub.
If (RecoContext Is Nothing) Then
Set RecoContext = New SpSharedRecoContext
Set grammar = RecoContext.CreateGrammar(1)
grammar.DictationLoad
End IfDebug.Print “Recognition Started”
grammar.DictationSetState (SpeechRuleState.SGDSActive)
Then in a sheet or module insert the following at the top, outside of a sub or function. Don’t put this inside a sub or function or otherwise the SR will deactivate as soon as the end of the sub or function is reached.
Dim srObject As New clsSpeech
Then in whichever subroutine or function you want to trigger the speech recognition insert the following. e.g. sub Worksheet_Activate()
Set srObject = New clsSpeech
Keep the VBA window open and visible but switch to the sheet you created the code in and you should see you’re speech being recognised in the VBA Intermediate Window.
Form version
This is very similar except all the event code now lives in the form and the SR remains ‘alive’ while the form is open.
Create a UserForm, e.g called UserForm and — for this demo — create a simple textbox called e.g.txtBox and optionally make it multiline.
At the top add the following two lines
Dim WithEvents RecoContext As SpSharedRecoContext
Dim grammar As ISpeechRecoGrammar
Insert the RecoContext_Recognition code - as for the class module version -except replacing Debug.Print strText with Me.txtBox = strText
Dim strText As String
strText = Result.PhraseInfo.GetText
Me.txtBox = strText
You could trigger the SR from a button or other event but for this example — again using the dropdowns at the top — select “UserForm” from the left and “Activate” from the right and paste the ‘initialisation’ code — as for the class module version - but changing Debug.Print “Recognition Started” to Me.txtBox = “Recognition Started”.
If (RecoContext Is Nothing) Then
Set RecoContext = New SpSharedRecoContext
Set grammar = RecoContext.CreateGrammar(1)
grammar.DictationLoad
End IfMe.txtBox = “Recognition Started”
grammar.DictationSetState (SpeechRuleState.SGDSActive)
Add a command button to a sheet and have it trigger UserForm.Show. When the form opens it will trigger the activate event which will start the SR.
You’re speech should be recognised and displayed in the text box.
Part 2— VBA Speech Recognition — SAPI STT — How do I stop it carrying out built-in commands automatically