SpObjectToken GetAttribute Method (SAPI 5.3)

Speech API 5.3
Microsoft Speech API 5.3

Object: SpObjectToken

GetAttribute Method

The GetAttribute method returns the value of the specified attribute.

The String returned contains the value for the Attribute. If the Attribute is present but does not contain additional information, the String will be Empty. If the Attribute is not present, SPERR_NOT_FOUND is returned. Not all engines support all attributes and it is possible to customize attributes for each engine.

     AttributeName As String
) As String


Specifies the AttributeName.

Return Value

The GetAttribute method returns a String variable.


In Visual Basic, attempting to access a nonexistent Attribute will cause a run-time error. Therefore, it is recommended to include an On Error statement trapping such cases.


This code example demonstrates the GetAttribute method. After creating an instance of a recognizer, the Recognizer property retrieves an SpObjectToken object whose GetAttribute method is called. The engine ID is displayed. Also two attributes are attempted to be displayed. The first is "VendorPreferred", an attribute that exists for the SAPI 5 SR engine. The other is "MyEngineAttribute", which should not be present. Attributes not found will cause a run-time error and as a result, require error handling code.

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

Option Explicit

Private Sub Form_Load()
    On Error GoTo EH

    Const NL = vbNewLine
    Dim objectToken As SpObjectToken
    Dim RC As SpSharedRecoContext
    Dim T As String
    Dim TokenName As String
    Dim Value As String

    Set RC = New SpSharedRecoContext
    Set objectToken = RC.Recognizer.Recognizer

    T = "Id: " & RC.Recognizer.Recognizer.Id & NL
    TokenName = "VendorPreferred"
    Value = objectToken.GetAttribute(TokenName)

    If Len(Value) = 0 Then
        T = T & TokenName & " attribute's value: Zero-length string" & NL
    End If

    TokenName = "MyEngineAttribute"
    On Error Resume Next
    T = TokenName & " : " & objectToken.GetAttribute(TokenName)

    If Err.Number Then
        T = T & "There is no attribute named 'MyEngineAttribute'."
    End If

    MsgBox T, vbInformation

    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 Sub