Adding voice commands to access the functionality of your application is a nice feature. But you may also want to access the same functionality from a menu or a toolbar button. Using separate procedures to handle voice commands makes that integration easy.
Start a new project, place an agent control on the form then add three command buttons named cmdHello, cmdDate and cmdTime. Set their caption property accordingly. Double click on the form to replace it's content with the following code.
Form1
Option Explicit
Dim miFormLeft%, miFormTop%
Dim msLocalPath$
Dim theAgent As IAgentCtlCharacter
Dim reqHB 'As IAgentCtlRequest
Private Sub Form_Load()
On Error Resume Next
CenterForm
LoadAgent "genie"
End Sub
Sub CenterForm()
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
miFormLeft = Me.Left \ Screen.TwipsPerPixelX
miFormTop = Me.Top \ Screen.TwipsPerPixelY
End Sub
Sub LoadAgent(sAgentName)
msLocalPath = "c:\program files\microsoft agent\characters\"
Agent1.Characters.Load sAgentName, msLocalPath & sAgentName & ".acs"
If Err = 0 Then
Set theAgent = Agent1.Characters(sAgentName)
theAgent.MoveTo miFormLeft - theAgent.Width, miFormTop - theAgent.Height
theAgent.Show
AddCommands
Else
MsgBox "Cannot load " & sAgentName
End
End If
End Sub
Sub AddCommands()
theAgent.Commands.Caption = "Commands Demo"
theAgent.Commands.Voice = "Commands Demo"
theAgent.Commands.Visible = True
theAgent.Commands.Add "hello", "Hello", "hello", True, True
theAgent.Commands.Add "date", "Date", "(what date is it | ...date...)", True, True
theAgent.Commands.Add "time", "Time", "(what time is it | ...time...)", True, True
End Sub
Private Sub Agent1_Command(ByVal UserInput As Object)
Select Case UserInput.Name
Case "hello": SayHello
Case "date": TellDate
Case "time": TellTime
End Select
End Sub
Sub SayHello()
theAgent.Speak "Hello, my name is " & theAgent.Name & "."
End Sub
Sub TellDate()
theAgent.Speak "The date is " & Format$(Date, "mmmm dd, yyyy") & "."
End Sub
Sub TellTime()
theAgent.Speak "The time is " & Time & "."
End Sub
Private Sub cmdDate_Click()
TellDate
End Sub
Private Sub cmdHello_Click()
SayHello
End Sub
Private Sub cmdTime_Click()
TellTime
End Sub
You will also note that the Date and Time commands are also triggered when the user says either words. You can get a complete reference of voice input definitions in theProgramming the Microsoft Agent Control documentation available from the Microsoft site.