HOWTO: Manipulate Office Assistants from Visual BasicID: Q173690
|
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:
Other errors may occur as well. These errors usually indicate an invalid use of a property or method, or a similar OLE automation error.Run-time error '-2147467259 (80004005)':
Operation cannot be performed when the application is inactive
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.
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:
The Office Assistant component must be installed using the Microsoft Office setup program before the step-by-step example will function correctly.Method 'assistant' of object '_application' failed
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
Additional query words:
Keywords : kbAutomation kbVBp400 kbVBp500
Version : WINDOWS:4.0,5.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: June 3, 1999