Share via


SpObjectToken Example (SAPI 5.3)

Microsoft Speech API 5.3

SpObjectToken Example

The following code example demonstrates the use of the Count property and the Item method. The example works for all objects returned as the collection ISpeechObjectTokens. The Count property returns the number of items in the collection. The Item method returns an individual member at the given index.

To run this code, create a form with no controls and paste this code into the Declarations section of the form.

Note The Item method and Count property behave the same way for all collections in the SAPI Automation object library that have them as members.

  
Option Explicit

Private Sub Form_Load()
    On Error GoTo EH

    Dim theResources As ISpeechObjectTokens
    Dim SharedRecognizer As SpSharedRecognizer

    Set SharedRecognizer = New SpSharedRecognizer
    Set theResources = SharedRecognizer.GetRecognizers
    Call DisplayItems(theResources)

    Set theResources = SharedRecognizer.GetAudioInputs
    Call DisplayItems(theResources)

    Set theResources = SharedRecognizer.GetProfiles
    Call DisplayItems(theResources)

EH:
    If Err.Number Then ShowErrMsg
End Sub

Sub DisplayItems(ByRef Resources As ISpeechObjectTokens)
    On Error GoTo EH

    Dim i As Long
    Dim recoObject As SpObjectToken
    Dim T As String

    For i = 0 To Resources.Count - 1
        Set recoObject = Resources.Item(i)
        T = T & recoObject.GetDescription & vbCrLf
    Next i

    MsgBox T, vbInformation

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub ShowErrMsg()

    ' Declare identifiers:
    Dim T As String

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

End Sub