Public WithEvents spLib As SpeechLib.SpInProcRecoContext
Public RecognizerGrammarRule As SpeechLib.ISpeechRecoGrammar
Public RecognizerGrammarRuleGrammarRule As SpeechLib.ISpeechGrammarRule
Public AppRunName As String
Public strDictation As String
Public strHypothesis As String
Public strRecognition As String
Public frm As frmDisplayChange
Public Function CreateMicrofon() As SpeechLib.SpObjectToken
    Dim objAudioTokenCategory As New SpeechLib.SpObjectTokenCategory
    Call objAudioTokenCategory.SetId("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\AudioInput", False)
    Dim objAudioToken As New SpeechLib.SpObjectToken
    Call objAudioToken.SetId(objAudioTokenCategory.Default, "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\AudioInput", False)
    Set CreateMicrofon = objAudioToken

End Function


Private Sub spLib_FalseRecognition(ByVal StreamNumber As Long, ByVal StreamPosition As Variant, ByVal Result As SpeechLib.ISpeechRecoResult)
    strHypothesis = "Error"
    'updCellMoveVoiceHypothesis (strHypothesis)
End Sub

Private Sub spLib_Hypothesis(ByVal StreamNumber As Long, ByVal StreamPosition As Variant, ByVal Result As SpeechLib.ISpeechRecoResult)
    Dim strText As String
'    If dicDictation.Exists(strDictation) = False Then
'        strHypothesis = "DicErr"
'        Exit Sub
'    End If
   
    strText = Result.PhraseInfo.getText(0, -1, True)
    strHypothesis = strText
    'updCellMoveVoiceHypothesis (strHypothesis)
    'Call updCellMoveVoiceRecognition(updItem, strRecognition)
   
End Sub

Private Sub CellSelect(ByVal strData As String)
    Dim arData() As String
    arData = Split(strData, ":")
    Cells(CInt(arData(0)), CInt(arData(1))).Select
End Sub
Private Sub spLib_Recognition(ByVal StreamNumber As Long, ByVal StreamPosition As Variant, ByVal RecognitionType As SpeechLib.SpeechRecognitionType, ByVal Result As SpeechLib.ISpeechRecoResult)
On Error GoTo err_spLib_Recognition
    Dim strText As String
    Dim dblRate As Double
    strText = Result.PhraseInfo.getText
    dblRate = Result.PhraseInfo.elements.Item(0).EngineConfidence
'    If dblRate <= 0.85 Then
'        Exit Sub
'    End If
'    If dicDictation.Exists(strDictation) = False Then
'        strHypothesis = "DicErr"
'        Exit Sub
'    End If
    'If InStr(strDictation, Result.PhraseInfo.Elements.Item(0).LexicalForm) > 0 Then Exit Sub
    Dim ret As Long
    Dim length  As Long
    Dim hWnd As Long
    Dim DisplayStr As String
    If Mid(strText, 1, 3) = "Get" Then
        hWnd = GetForegroundWindow
        length = SendMessage(GetForegroundWindow, WM_GETTEXTLENGTH, 0, 0)
        If length > 10000 Then
            length = 10000
        End If
        DisplayStr = String(length, vbNullChar)
        ret = SendMessageStr(GetForegroundWindow, WM_GETTEXT, length + 1, DisplayStr)
    End If
    Select Case strText
        Case "Get001"
            frm.txt001GetHundle.Text = hWnd
            frm.txt001Title.Text = DisplayStr
        Case "Get002"
            frm.txt002GetHundle.Text = hWnd
            frm.txt002Title.Text = DisplayStr
        Case "Get003"
            frm.txt003GetHundle.Text = hWnd
            frm.txt003Title.Text = DisplayStr
        Case "Get004"
            frm.txt004GetHundle.Text = hWnd
            frm.txt004Title.Text = DisplayStr
        Case "Get005"
            frm.txt005GetHundle.Text = hWnd
            frm.txt004Title.Text = DisplayStr
        Case "Top001"
            hWnd = CLng(frm.txt001GetHundle.Text)
        Case "Top002"
            hWnd = CLng(frm.txt002GetHundle.Text)
        Case "Top003"
            hWnd = CLng(frm.txt003GetHundle.Text)
        Case "Top004"
            hWnd = CLng(frm.txt004GetHundle.Text)
        Case "Top005"
            hWnd = CLng(frm.txt005GetHundle.Text)
    End Select
    If Mid(strText, 1, 3) = "Top" Then
        SetForceForegroundWindow hWnd
        BringWindowToTop hWnd
    End If
    Exit Sub
err_spLib_Recognition:
    strRecognition = ""
End Sub
Function intData(ByVal Data As String) As Integer
    If Data = "" Then
        intData = 1
    Else
        intData = CInt(Data)
    End If
End Function
Private Sub spLib_StartStream(ByVal StreamNumber As Long, ByVal StreamPosition As Variant)
    strRecognition = ""
    strHypothesis = ""
    'updCellMoveVoiceHypothesis (strHypothesis)
    'Call updCellMoveVoiceRecognition(updItem, strRecognition)
End Sub
Private Sub Class_Initialize()
On Error GoTo err_Class_Initialize
    Dim recoperson As SpObjectToken
    Dim strlanguage As String
    Dim blnHit As Boolean
    blnHit = False
    Set spLib = New SpeechLib.SpInProcRecoContext
    Set spLib.Recognizer.AudioInput = CreateMicrofon
     
   
   
    spLib.CmdMaxAlternates = 0
    '言語モデルの作成
    Set RecognizerGrammarRule = spLib.CreateGrammar
   
    RecognizerGrammarRule.Reset
    Dim RuleState As ISpeechGrammarRuleState
    '言語モデルのルールのトップレベルを作成する
    Set RecognizerGrammarRuleGrammarRule = RecognizerGrammarRule.rules.Add("TopLevelRule", SRATopLevel)
    RecognizerGrammarRuleGrammarRule.InitialState.AddWordTransition Nothing, "/Get001/げっといち/ゲットイチ;"
    RecognizerGrammarRuleGrammarRule.InitialState.AddWordTransition Nothing, "/Get002/げっとに/ゲットニ;"
    RecognizerGrammarRuleGrammarRule.InitialState.AddWordTransition Nothing, "/Get003/げっとさん/ゲットサン;"
    RecognizerGrammarRuleGrammarRule.InitialState.AddWordTransition Nothing, "/Get004/げっとよん/ゲットヨン;"
    RecognizerGrammarRuleGrammarRule.InitialState.AddWordTransition Nothing, "/Get005/げっとご/ゲットゴ;"
    RecognizerGrammarRuleGrammarRule.InitialState.AddWordTransition Nothing, "/Top001/とっぷいち/トップイチ;"
    RecognizerGrammarRuleGrammarRule.InitialState.AddWordTransition Nothing, "/Top002/とっぷに/トップニ;"
    RecognizerGrammarRuleGrammarRule.InitialState.AddWordTransition Nothing, "/Top003/とっぷさん/トップサン;"
    RecognizerGrammarRuleGrammarRule.InitialState.AddWordTransition Nothing, "/Top004/とっぷよん/トップヨン;"
    RecognizerGrammarRuleGrammarRule.InitialState.AddWordTransition Nothing, "/Top005/とっぷご/トップゴ;"
    
    RecognizerGrammarRule.rules.Commit
    '音声認識開始。(トップレベルのオブジェクトの名前で SpeechRuleState.SGDSActive を指定する.)
    RecognizerGrammarRule.CmdSetRuleState "TopLevelRule", SpeechRuleState.SGDSActive

    Exit Sub
err_Class_Initialize:
    MsgBox Err.Number
End Sub