Microsoft & .NETVisual BasicUsing Microsoft Agent

Using Microsoft Agent

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

They’re annoying, ship with a dozen ear-piercing sound effects and crop up at the most inconvenient times yet are still billed as the ‘help file of the future’.

I’m talking about the Office animated assistants, including that oh-so-popular Paperclip.

And now, thanks to a series of free Microsoft downloads, you too can add such functionality (however irritating) direct to your own Visual Basic applications!

In this article, we’ll be skimming over all the main features of the ‘Microsoft Agent’, plus present you with a big bag of sneaky code snippets – allowing you to start colourfully bugging your users in no time.

Oh deary, what a refreshing change…

First off, you’ll need to download and install Agent from the Microsoft website at www.microsoft.com/msagent/ – the latest version at time of writing was Agent 2.0, on which this guide is based.

The Microsoft Agent Core Components download comes in at a reasonable 395K, a self-extracting executable that must be run on each computer you wish to use such animations.

However this is merely the engine that ‘runs’ the characters. The actual animations themselves must be obtained separately. At time of writing, Microsoft had four freely available characters Genie (1.6MB), Merlin (1.8MB), Robby (2.2MB), and Peedy (3.3MB).

To use this tutorial, you must download the main agent kit and at least one character.

You might also want to check out the available L&H TruVoice Text-to-Speech engine, which allows your characters to not only display words but also speak them. Be prepared for a Stephen Hawking-like rendition however… not brilliant, but hey, it’s free!

So, you’ve downloaded Agent and are ready to rock-and-roll?

Great! First let’s add our Agent to a sample project:

  • Create a new Visual Basic project
  • Click ‘Project’, ‘References’
  • Find and select ‘Microsoft Agent Control 2.0’

Now let’s get it to talk:

  • Create the following public variables in the ‘General Declarations’ section behind your Form:
Dim WithEvents CoreAgent As AgentDim Assistant As IAgentCtlCharacterEx
  • Add a Command Button to Form1
  • Change it’s Caption to "Initialise Agent"
  • Insert the following code behind the Command Button:
Set CoreAgent = New AgentCoreAgent.Characters.Load "Assistant"Set Assistant = CoreAgent.Characters("Assistant")

This code simply ‘fires up’ the agent. The first line creates the main Agent engine object. From this, the second line loads the default character, christening it a very original "Assistant". Finally, the third line of code sets your Assistant object to the newly created character.

It’s worth noting that from this point onwards, you only really need to play around with the Assistant object. We’ve finished our work with the CoreAgent object.

Top Tip: It’s worth noting the second line of code also has an optional parameter ‘LoadKey’. If you have more than one Agent character on your computer, this is where you can specify which one to use. Just insert the character filename (and a path if not in WindowsMsAgentChars), such as "Robby.acs", for example: CoreAgent.Characters.Load “Assistant”, “Robby.acs”

Let’s continue with the project:

  • Add another Command Button to Form1
  • Change it’s Caption to "Start Agent"
  • Insert the following code behind the Command Button:
With Assistant   .Show

.Speak ("Hello! I'm your friendly assistant, Robby!") .Think ("I wonder if he's listening?") .HideEnd With

These fairly guessable methods bring your character to life. Let’s see them in action now:

  • Press F5 to run your application
  • Hit the Command Button to test your code

Hopefully the agent should appear on your screen and the passed text be displayed in a speech balloon. If you’ve installed the Text-to-Speech engine, you’ll even hear the Agent speak to you!

Finally, the agent will think out your I-wonder-if-he’s-listening sentence in an idea bubble, before finally disappearing. Wahoo!

Top Tip: To change the font used by the speech balloon, set the Assistant.Balloon.FontName property, like this: Assistant.Balloon.FontName = “Verdana”

In addition to talking, why not get your agent walking?

You can instruct your agent to perform an animation by running the Play method.

Here’s how to make your agent read:

Assistant.Play ("Read")

Or perhaps you’d like to make him think?

Assistant.Play ("Think")

How about a quick wave?

Assistant.Play ("Wave")

These are just three generic animations you could use, though each character has it’s own individual actions. To find out what your agent can do, try running this sneaky code snippet:

Dim Animation As VariantFor Each Animation In Assistant.AnimationNamesMsgBox AnimationNext

This displays a list of all animations your character supports. When you have the animation name, just pass it as an argument to the Play method.

It’s worth noting that after playing the animation, most characters go back to their default rest mode. However some animations, such as ‘Reading’ are continuous and don’t cease until you execute the Stop method.

Sometimes, that darn Text-to-Speech engine can sound a little monotone, not reflecting the emphasis and strain we human beings place on certain words.

You can get around this using speech output tags, a method of telling the Agent to put a little more effort into certain words.

To use, simply insert emp before the word(s) you want emphasised.

This sample sentence, for example, emphasises the word ‘very’:

Assistant.Speak ("He is bad. empVery bad.")

>

Try it both with and without the emp tag. You’ll soon notice the difference!

Another little-known trick is to try inserting Chr="Whisper"

Doing this makes your Agent whisper the sentence. Let’s look at an example:

Assistant.Speak ("Chr=""Whisper""But don't tell him I told you!")

Not bad for a computer!

Let’s quickly review how to manipulate Microsoft Agent with this printer-friendly rundown:

  • To ‘initialise’ your Agent character:
Dim Assistant As IAgentCtlCharacterExAgentControlOnForm.Characters.Load "Assistant"Set Assistant = Agent1.Characters("Assistant")
  • To show or hide your Agent character:
Assistant.ShowAssistant.Hide
  • To make your Agent character talk:
Assistant.Speak ("Speech Text Goes Here")
  • To make your Agent character think:
Assistant.Think ("Think Text Goes Here")
  • To animate your character:
Assistant.Play("AnimationName")
  • To find out what animations your character is capable of:
Dim Animation As VariantFor Each Animation In Assistant.AnimationNames   MsgBox AnimationNext
  • To place emphasis on a particular word:
Assistant.Speak ("He is bad. empVery bad.")
  • To whisper a sentence:
Assistant.Speak ("Chr=""Whisper""But don't tell him I told you!")

In this mini-guide, we covered the basics of using Microsoft Agent to get your own mini assistant up and running. We explored basic agent techniques, character animation, text-to-speech technologies, plus word emphasis. The article then concluded with a quick printer-friendly code rundown.

If you’re interested in taking your work with Agent even further, be sure to thoroughly check out the official website at www.microsoft.com/msagent/

You might also be interested in one wonderful article originally published by Visual Basic Developer magazine, showing you how to add speech recognition qualities to your agent, whilst wrapping it all up into a nice neat class. Read it online – msdn.microsoft.com/library/periodic/period00/vb00f7.htm

Well, that’s about all for this tutorial it simply remains for me to wish you all the best in using Agent oh, and I sincerely hope you have a real ol’ laugh annoying them thar users.

Hehe…Toodle-pip!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories