VB Application Sample: Dictation Recognition (Shared) (SAPI 5.4)

Speech API 5.4
Microsoft Speech API 5.4

VB Application Sample: Dictation Recognition (Shared)

The following code sample represents a simple, but functional, recognition application. It uses a dictation grammar and allows free dictation. The commented lines refer to hypothetical text boxes in a form to possibly display information. Of course you may modify this application as needed to fit your own requirements.

Before running the application, a speech reference must be included. Using the Project->References menu, find and select the Microsoft Speech Object Library.

Public WithEvents RC As SpSharedRecoContext
Public myGrammar As ISpeechRecoGrammar

Private Sub Form_Load()
    On Error GoTo EH

    Set RC = New SpSharedRecoContext

    Set myGrammar = RC.CreateGrammar
    myGrammar.DictationSetState SGDSActive

    If Err.Number Then ShowErrMsg
End Sub

Private Sub RC_Recognition(ByVal StreamNumber As Long, ByVal StreamPosition As Variant, ByVal RecognitionType As SpeechLib.SpeechRecognitionType, ByVal Result As SpeechLib.ISpeechRecoResult)
    'Label1.Caption = Result.PhraseInfo.GetText
End Sub

Private Sub RC_StartStream(ByVal StreamNumber As Long, ByVal StreamPosition As Variant)
    'Label2.Caption = Val(StreamNumber)
End Sub

Private Sub ShowErrMsg()

    ' Declare identifiers:
    Const NL = vbNewLine
    Dim T As String

    T = "Desc: " & Err.Description & NL
    T = T & "Err #: " & Err.Number
    MsgBox T, vbExclamation, "Run-Time Error"

End Sub