|
 |
 |
 |
 |
Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms
of Use |
Trademarks
Article ID: 308409 - Last Review: January 17, 2007 - Revision: 8.4 PRB: Visual Basic .NET Error Using GetObject or GetActiveObject for Running Instance of Office ApplicationThis article was previously published under Q308409 For a Microsoft Visual Basic 6.0 version of this
article, see
238610Â
(http://kbalertz.com/Feedback.aspx?kbNumber=238610/EN-US/
)
. For a Microsoft Visual C#
.NET version of this article, see
316125Â
(http://kbalertz.com/Feedback.aspx?kbNumber=316125/EN-US/
)
. When you try to use GetObject or System.Runtime.InteropServices.Marshal.GetActiveObject from Microsoft Visual Basic .NET to automate a running Microsoft Office
application, you may receive one of the following error messages:
With GetObject: Cannot create ActiveX component.
With System.Runtime.InteropServices.Marshal.GetActiveObject: An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
Additional information: Operation unavailable
Although the Office application is running, it may not be
registered in the Running Object Table (ROT). A running instance of an Office
application must be registered in the ROT before before it can be
automated. When an Office application starts, it does not immediately
register its running objects. This optimizes the application startup process.
Instead of registering at startup, an Office application registers its running
objects in the ROT after it loses focus. Therefore, if you attempt to attach to
a running instance of an Office application before the application has lost
focus, you may receive an error message. Using code, you can change focus from the Office
application to your own application (or to some other application) to allow it
to register itself in the ROT. Additionally, if your code is starting the
executable (.exe) file for the Office application, you may need to wait for the
Office application to finish loading before you attempt to attach to the
running instance. A code sample is provided as a workaround in the "More
Information" section.
This behavior is by design. In most situations, developers who want to automate an
Office application should use CreateObject (or New) to start a new instance of the Office application. In some
cases, however, you may prefer to automate an Office application that is
already running, such as if the user previously started the Office application
or if you start the .exe file for the Office application by using code so that
you can specify command-line switches for the application. In order to automate
the running Office application, you must use GetObject. Steps to Reproduce Behavior- Start Microsoft Visual Studio .NET.
- On the File menu, click New, and then click Project. Under Project types, click Visual Basic Projects, and then click Windows Application under Templates. Form1 is created by default.
- On the View menu, click Toolbox to display the toolbox, and then add a button to Form1.
- Double-click Button1. The code window for the form appears.
- In the code window, replace the following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
with:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oExcel As Object
' Start a new instance of Microsoft Excel.
Shell("C:\Program Files\Microsoft Office\Office10\Excel.exe", _
AppWinStyle.MinimizedNoFocus)
' "Cannot create ActiveX component." occurs on the following line:
oExcel = GetObject(, "Excel.Application")
MsgBox(oExcel.Name, MsgBoxStyle.MsgBoxSetForeground)
oExcel = Nothing
End Sub
- Make sure that the location of the Excel.exe file is
correct in the code sample.
- Quit Microsoft Excel if it is already running.
- On the Debug menu, click Start.
To work around the problem, follow these steps:
- Give focus to the Office application by changing the
second argument of the Shell function to AppWinStyle.MinimizedFocus, AppWinStyle.MaximizedFocus, or AppWinStyle.NormalFocus.
- Give your Visual Basic form the focus.
- Try to use GetObject while accounting for the load time of the Office
application.
The following revised code illustrates this workaround:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iSection As Integer
Dim iTries As Integer
Dim oExcel As Object
' Enable an error handler for this procedure.
On Error GoTo ErrorHandler
' Start Excel, giving it focus.
Shell("C:\Program Files\Microsoft Office\Office10\Excel.exe", _
AppWinStyle.MinimizedFocus)
' Move focus back to this form. (This ensures the Office
' application registers itself in the ROT, allowing
' GetObject to find it.)
AppActivate(Title:=Me.Text)
' Attempt to use GetObject to reference the running
' Office application.
iSection = 1 'attempting GetObject...
oExcel = GetObject(, "Excel.Application")
iSection = 0 'resume normal error handling
' Automate Excel.
oExcel.ActiveCell.Value = "Hi"
MsgBox(oExcel.Name & ": able to GetObject after " & _
iTries + 1 & " tries.", MsgBoxStyle.MsgBoxSetForeground)
' You are finished with automation, so release your reference.
oExcel = Nothing
' Exit procedure.
Exit Sub
ErrorHandler:
If iSection = 1 Then 'GetObject may have failed because the
'Shell function is asynchronous; enough time has not elapsed
'for GetObject to find the running Office application. Wait
'1/2 seconds and retry the GetObject. If you try 20 times
'and GetObject still fails, assume some other reason
'for GetObject failing and exit the procedure.
iTries = iTries + 1
If iTries < 20 Then
System.Threading.Thread.Sleep(500) ' wait 1/2 seconds
AppActivate(Title:=Me.Text)
Resume 'resume code at the GetObject line
Else
MsgBox("GetObject still failing. Process ended.", _
MsgBoxStyle.MsgBoxSetForeground)
End If
Else 'iSection = 0 so use normal error handling:
MsgBox(Err.Description, _
MsgBoxStyle.MsgBoxSetForeground)
End If
End Sub
For more information, visit the following Microsoft Developer
Network (MSDN) Web site:
APPLIES TO- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft Office Access 2003
- Microsoft Access 2002 Standard Edition
- Microsoft Office Excel 2003
- Microsoft Excel 2002 Standard Edition
- Microsoft Office PowerPoint 2003
- Microsoft PowerPoint 2002 Standard Edition
- Microsoft Office Word 2003
- Microsoft Word 2002
| kbautomation kbprb KB308409 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
|
 |
 |
 |
 |
 |
 |
 |
| |