Microsoft KB Archive/173690: Difference between revisions
m (Text replacement - ">" to ">") |
m (Text replacement - """ to """) |
||
Line 50: | Line 50: | ||
== SUMMARY == | == 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 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: | ||
<div class="errormessage"> | <div class="errormessage"> | ||
Line 65: | Line 65: | ||
<pre class="codesample">Assistant.Visible = True | <pre class="codesample">Assistant.Visible = True | ||
</pre> | </pre> | ||
To programmatically manipulate the Microsoft Office Assistants from within Visual Basic, you must create a reference to an active Office application object, such as | 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.<br /> | ||
<br /> | <br /> | ||
'''NOTE:''' In Office 2000 and later, the Office Assistants use MSAgent technology. The methods for automating assistants in Office 2000 differ from those described in this article. | '''NOTE:''' In Office 2000 and later, the Office Assistants use MSAgent technology. The methods for automating assistants in Office 2000 differ from those described in this article. | ||
Line 74: | Line 74: | ||
== MORE INFORMATION == | == 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 | 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.<br /> | ||
<br /> | <br /> | ||
'''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: | '''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: | ||
Line 88: | Line 88: | ||
<ol> | <ol> | ||
<li>Start a new Standard EXE project. Form1 is created by default.</li> | <li>Start a new Standard EXE project. Form1 is created by default.</li> | ||
<li>Add references to the | <li>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.</li> | ||
<li>Add a Timer control (Timer1) to Form1.</li> | <li>Add a Timer control (Timer1) to Form1.</li> | ||
<li>Set Timer1's Interval property to 2000.</li> | <li>Set Timer1's Interval property to 2000.</li> | ||
Line 94: | Line 94: | ||
<pre class="codesample">Option Explicit | <pre class="codesample">Option Explicit | ||
#Const OfficeAppObject = | #Const OfficeAppObject = "Microsoft Access" 'Or "Microsoft Word" or | ||
' | ' "Microsoft Excel" or "Microsoft PowerPoint" | ||
'Due to limitations in the Microsoft Outlook object model, you | 'Due to limitations in the Microsoft Outlook object model, you | ||
'currently cannot manipulate the Office Assistant using a Microsoft | 'currently cannot manipulate the Office Assistant using a Microsoft | ||
'Outlook object. | 'Outlook object. | ||
#If OfficeAppObject = | #If OfficeAppObject = "Microsoft Access" Then | ||
'Establish a reference to | 'Establish a reference to "Microsoft Access 8.0 Object Library" | ||
Dim objOffice As New Access.Application | Dim objOffice As New Access.Application | ||
#ElseIf OfficeAppObject = | #ElseIf OfficeAppObject = "Microsoft Word" Then | ||
'Establish a reference to | 'Establish a reference to "Microsoft Word 8.0 Object Library" | ||
Dim objOffice As New Word.Application | Dim objOffice As New Word.Application | ||
#ElseIf OfficeAppObject = | #ElseIf OfficeAppObject = "Microsoft Excel" Then | ||
'Establish a reference to | 'Establish a reference to "Microsoft Excel 8.0 Object Library" | ||
Dim objOffice As New Excel.Application | Dim objOffice As New Excel.Application | ||
#ElseIf OfficeAppObject = | #ElseIf OfficeAppObject = "Microsoft PowerPoint" Then | ||
'Establish a reference to | 'Establish a reference to "Microsoft PowerPoint 8.0 Object | ||
'Library | 'Library". NOTE: With this example, PowerPoint will become active | ||
'and visible when executed. | 'and visible when executed. | ||
Dim objOffice As New PowerPoint.Application | Dim objOffice As New PowerPoint.Application | ||
Line 126: | Line 126: | ||
With objOffice.Assistant | With objOffice.Assistant | ||
bAssistantVisibility = .Visible | bAssistantVisibility = .Visible | ||
msg = | msg = "The Office Assistant Visible Property is set to" _ | ||
& Str$(bAssistantVisibility) & | & Str$(bAssistantVisibility) & "." & vbCrLf & vbCrLf _ | ||
& | & "Do you want to see some Office Assistant Animation?" | ||
iChoice = MsgBox(msg, vbYesNo, .Name) | iChoice = MsgBox(msg, vbYesNo, .Name) | ||
Select Case iChoice | Select Case iChoice | ||
Case vbYes | Case vbYes | ||
#If OfficeAppObject = | #If OfficeAppObject = "Microsoft Access" Then | ||
AppActivate | AppActivate "Microsoft Access", False | ||
.Visible = True | .Visible = True | ||
#ElseIf OfficeAppObject = | #ElseIf OfficeAppObject = "Microsoft Word" Then | ||
objOffice.WindowState = wdWindowStateMinimize | objOffice.WindowState = wdWindowStateMinimize | ||
objOffice.Activate | objOffice.Activate | ||
#ElseIf OfficeAppObject = | #ElseIf OfficeAppObject = "Microsoft Excel" Then | ||
objOffice.WindowState = xlMinimized | objOffice.WindowState = xlMinimized | ||
objOffice.Visible = True | objOffice.Visible = True | ||
.Visible = True | .Visible = True | ||
#ElseIf OfficeAppObject = | #ElseIf OfficeAppObject = "Microsoft PowerPoint" Then | ||
objOffice.Activate | objOffice.Activate | ||
.Visible = True | .Visible = True | ||
Line 148: | Line 148: | ||
Call subAnimation | Call subAnimation | ||
Case vbNo | Case vbNo | ||
#If OfficeAppObject <> | #If OfficeAppObject <> "Microsoft Access" Then | ||
objOffice.Quit | objOffice.Quit | ||
#End If | #End If | ||
Line 168: | Line 168: | ||
Case 7: .Animation = msoAnimationThinking | Case 7: .Animation = msoAnimationThinking | ||
Case 8: .Animation = msoAnimationWritingNotingSomething | Case 8: .Animation = msoAnimationWritingNotingSomething | ||
Case Else: MsgBox | Case Else: MsgBox "Random number generator error.", _ | ||
vbExclamation, | vbExclamation, "When I get 64" | ||
End Select | End Select | ||
End With | End With |
Revision as of 11:08, 21 July 2020
Article ID: 173690
Article Last Modified on 2/21/2005
APPLIES TO
- Microsoft Visual Basic 5.0 Control Creation Edition
- Microsoft Visual Basic 5.0 Learning Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Visual Basic 4.0 Standard Edition
- Microsoft Visual Basic 4.0 Professional Edition
- Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
This article was previously published under Q173690
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:
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 and later, the Office Assistants use 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:
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.
- Start a new Standard EXE project. Form1 is created by default.
- 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.
- Add a Timer control (Timer1) to Form1.
- Set Timer1's Interval property to 2000.
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
- Press F5 to run the program and wait. After a few seconds the first prompt will appear. Click Yes to watch the Assistant animation.
Keywords: kbhowto kbautomation KB173690