HOWTO: Manipulate Office Assistants from Visual Basic

ID: Q173690


The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0


SUMMARY

Microsoft Office 97 introduces the Office Assistant, a cartoon-like character designed to answer questions and help you perform routine tasks with Microsoft Office applications. Office Assistants have an object model available in the "Microsoft Office 8.0 Object Library" (Mso97.dll) and it appears that the Assistant objects may be manipulated similar to any other OLE object. However, attempting to manipulate the Assistants from Visual Basic may cause the following error:

Run-time error '-2147467259 (80004005)':
Operation cannot be performed when the application is inactive
Other errors may occur as well. These errors usually indicate an invalid use of a property or method, or a similar OLE automation error.

The Office Assistant object model is read-only when used outside of Microsoft Office applications. Properties of the Office Assistant may be read; for example, the following line of code returns true or false:

Msgbox Assistant.Visible 
However, Office Assistant properties cannot be changed or set, and methods may not be executed from within Visual Basic or any other development environment without a reference to an active Office application object. For example, executing the following line causes the error described above:

Assistant.Visible = True 
To programmatically manipulate the Microsoft Office Assistants from within Visual Basic, you must create a reference to an active Office application object, such as "Access.Application," from which you can manipulate the Assistant.

NOTE: In Office 2000, the Office Assistants utilize MSAgent technology. The methods for automating assistants in Office 2000 differ from those described in this article.


MORE INFORMATION

The step-by-step example below illustrates how to manipulate the Microsoft Office Assistant using Microsoft Office application objects. When the following code executes, an Office application object is instantiated and the Office application will be running and visible on the Task Bar. The user is prompted approximately every two seconds to view some Office Assistant animation. If the user responds "Yes" to the message box, a random Office Assistant animation is selected and executed.

NOTE: The Office Assistant component is optional when setting up Microsoft Office applications. If the Office Assistants are not installed, a run-time error will occur with the following message:

Method 'assistant' of object '_application' failed
The Office Assistant component must be installed using the Microsoft Office setup program before the step-by-step example will function correctly.

Step-By-Step Example

In the sample code below, the objOffice variable is used to get a reference to an Office application object. Remember that a reference to an Office application object is necessary; the method below is the way to do this in Visual Basic. If you would like to substitute another Office application for Microsoft Access, modify the OfficeAppObject constant to be Microsoft Word, Microsoft Excel or Microsoft PowerPoint.
  1. Start a new Standard EXE project. Form1 is created by default.


  2. Add references to the "Microsoft Office 8.0 Object Library" and the "Microsoft Access 8.0 Object Library". In Visual Basic 5.0 click References on the Project menu; in Visual Basic 4.0 click References on the Tools menu. "Microsoft Word 8.0 Object Library," "Microsoft Excel 8.0 Object Library," and "Microsoft PowerPoint 8.0 Object Library" could also be selected.


  3. Add a Timer control (Timer1) to Form1.


  4. Set Timer1's Interval property to 2000.


  5. Insert the following code into the General Declarations section of Form1:
    
    Option Explicit
    
          #Const OfficeAppObject = "Microsoft Access" 'Or "Microsoft Word" or
                                 ' "Microsoft Excel" or "Microsoft PowerPoint"
          'Due to limitations in the Microsoft Outlook object model, you
          'currently cannot manipulate the Office Assistant using a Microsoft
          'Outlook object.
    
          #If OfficeAppObject = "Microsoft Access" Then
             'Establish a reference to "Microsoft Access 8.0 Object Library"
             Dim objOffice As New Access.Application
          #ElseIf OfficeAppObject = "Microsoft Word" Then
             'Establish a reference to "Microsoft Word 8.0 Object Library"
             Dim objOffice As New Word.Application
          #ElseIf OfficeAppObject = "Microsoft Excel" Then
             'Establish a reference to "Microsoft Excel 8.0 Object Library"
             Dim objOffice As New Excel.Application
          #ElseIf OfficeAppObject = "Microsoft PowerPoint" Then
             'Establish a reference to "Microsoft PowerPoint 8.0 Object
             'Library". NOTE: With this example, PowerPoint will become active
             'and visible when executed.
             Dim objOffice As New PowerPoint.Application
          #Else
             'If OfficeAppObject is NOT one of the four above items, a compiler
             'error will occur!
          #End If
    
          Private Sub Timer1_Timer()
             Dim bAssistantVisibility As Boolean
             Dim msg As String
             Dim iChoice As Integer
    
             With objOffice.Assistant
                bAssistantVisibility = .Visible
                msg = "The Office Assistant Visible Property is set to" _
                    & Str$(bAssistantVisibility) & "." & vbCrLf & vbCrLf _
                    & "Do you want to see some Office Assistant Animation?"
                iChoice = MsgBox(msg, vbYesNo, .Name)
                Select Case iChoice
                   Case vbYes
                      #If OfficeAppObject = "Microsoft Access" Then
                         AppActivate "Microsoft Access", False
                         .Visible = True
                      #ElseIf OfficeAppObject = "Microsoft Word" Then
                         objOffice.WindowState = wdWindowStateMinimize
                         objOffice.Activate
                      #ElseIf OfficeAppObject = "Microsoft Excel" Then
                         objOffice.WindowState = xlMinimized
                         objOffice.Visible = True
                         .Visible = True
                      #ElseIf OfficeAppObject = "Microsoft PowerPoint" Then
                         objOffice.Activate
                         .Visible = True
                      #End If
                      Call subAnimation
                   Case vbNo
                      #If OfficeAppObject <> "Microsoft Access" Then
                         objOffice.Quit
                      #End If
                      Set objOffice = Nothing
                   End
                End Select
             End With
          End Sub
    
          Private Sub subAnimation()
             With objOffice.Assistant
                Select Case Int((8 * Rnd) + 1)
                   Case 1: .Animation = msoAnimationCheckingSomething
                   Case 2: .Animation = msoAnimationGetTechy
                   Case 3: .Animation = msoAnimationSearching
                   Case 4: .Animation = msoAnimationWorkingAtSomething
                   Case 5: .Animation = msoAnimationGetArtsy
                   Case 6: .Animation = msoAnimationSaving
                   Case 7: .Animation = msoAnimationThinking
                   Case 8: .Animation = msoAnimationWritingNotingSomething
                   Case Else: MsgBox "Random number generator error.", _
                                     vbExclamation, "When I get 64"
                End Select
             End With
          End Sub 


  6. Press F5 to run the program and wait. After a few seconds the first prompt will appear. Click Yes to watch the Assistant animation.


Additional query words:

Keywords : kbAutomation kbVBp400 kbVBp500
Version : WINDOWS:4.0,5.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: June 3, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.