ISpeechRecoGrammar RecoContext property (SAPI 5.3)

Microsoft Speech API 5.3

Interface: ISpeechRecoGrammar

RecoContext Property

The RecoContext property returns the RecoContext object that created the grammar.


Set: Not available.
Get: ISpeechRecoContext = ISpeechRecoGrammar.RecoContext


  • ISpeechRecoGrammar
    The owning object.
  • ISpeechRecoContext
    Set: (This property is read-only).
    Get: An ISpeechRecoContext object which instantiates the RecoContext object that created the grammar.


The following Visual Basic form code demonstrates the use of the Id and RecoContext properties. To run this code, create a form with the following controls:

  • Two command buttons called Command1 and Command2

Paste this code into the Declarations section of the form.

The Form_Load procedure creates a recognition context with two ISpeechRecoGrammar objects. The ISpeechRecoGrammar objects are created with Id properties of 6 and 7. The Command1 and Command2 procedures each send one of the grammar objects to the GrammarSub procedure as parameters. The GrammarSub procedure displays the ID of the grammar object parameter, and uses the parameter's RecoContext property to pause and resume the recognition context that owns the grammar.

There is no special significance to the Id property values of 6 or 7; these values are arbitrary. Additionally, the Pause and Resume methods in the GrammarSub procedure are intended simply to show how the grammar's RecoContext property provides access to the methods and properties of the recognition context that created the grammar.

Option Explicit

Dim RecoContext As SpeechLib.SpSharedRecoContext
Dim objGR1 As SpeechLib.ISpeechRecoGrammar
Dim objGR2 As SpeechLib.ISpeechRecoGrammar

Private Sub Command1_Click()
    Call GrammarSub(objGR1)
End Sub

Private Sub Command2_Click()
    Call GrammarSub(objGR2)
End Sub

Private Sub Form_Load()
    On Error GoTo EH

    Set RecoContext = New SpSharedRecoContext
    Set objGR1 = RecoContext.CreateGrammar(6)       'Create grammar with ID = 6
    Set objGR2 = RecoContext.CreateGrammar("7")     'Create grammar with ID = 7

    If Err.Number Then ShowErrMsg
End Sub

Private Sub GrammarSub(G As SpeechLib.ISpeechRecoGrammar)
    On Error GoTo EH

    G.RecoContext.Pause     'Pause the RecoContext that owns grammar
    MsgBox G.Id             'Display the Id
    G.RecoContext.Resume    'Resume the RecoContext

    If Err.Number Then ShowErrMsg
End Sub

Private Sub ShowErrMsg()

    ' Declare identifier:
    Dim T As String

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

End Sub