Partager via


ISpeechRecoGrammar CmdLoadFromFile method (SAPI 5.3)

Microsoft Speech API 5.3

Interface: ISpeechRecoGrammar

CmdLoadFromFile Method

The CmdLoadFromFile method loads a command and control grammar from the specified file.

The grammar may be compiled or uncompiled, and it can be loaded for static or dynamic use, as specified in the LoadOption parameter.

  
    ISpeechRecoGrammar.CmdLoadFromFile(
     FileName As String,
     [LoadOption As SpeechLoadOption = SLOStatic]
)

Parameters

  • FileName
    Specifies the file name. SAPI 5 supports loading of compiled static grammars through a URL.
  • LoadOption
    [Optional] Specifies whether the grammar is to be loaded for static or dynamic use. The default is static.

Return Value

None.

Example

The following Visual Basic form code demonstrates the use of the CmdLoadFromFile and the CmdLoadFromMemory methods. To run this code, create a form with the following control:

  • A command button called Command1

Paste this code into the Declarations section of the form.

The Form_Load procedure creates two grammar objects and uses the CmdLoadFromFile method to load the Solitaire rules into the first grammar. The Command1_Click procedure calls a subroutine called GrammarToMemory, which creates a temporary grammar in a Variant variable, and returns this grammar to the caller. The Command1 procedure then reloads the first grammar with the temporary grammar.

  
Option Explicit

Dim C As SpeechLib.SpSharedRecoContext
Dim G1 As SpeechLib.ISpeechRecoGrammar
Dim G2 As SpeechLib.ISpeechRecoGrammar

Private Sub Command1_Click()
    Dim GT As Variant               'Temp grammar in Variant variable

    On Error GoTo EH

    GT = GrammarToMemory(G1)         'GT is temp version of grammar G
    Call G2.CmdLoadFromMemory(GT, SLOStatic)

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub Form_Load()
    On Error GoTo EH

    Set C = New SpSharedRecoContext
    Set G1 = C.CreateGrammar
    Set G2 = C.CreateGrammar

    Call G1.CmdLoadFromFile("c:\sol.xml", SLODynamic)

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Function GrammarToMemory(objGRM As SpeechLib.ISpeechRecoGrammar) As Variant
    On Error GoTo EH

    'Make changes to a standard grammar, and save it as temp grammar.
    'Add rules to grammar, delete rules from grammar, or other changes here.

    'Return the variant from ISpeechGrammarRules.CommitAndSave
    GrammarToMemory = objGRM.Rules.CommitAndSave("")

EH:
    If Err.Number Then ShowErrMsg
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

End Sub