ACC: Automation Object Disappears When Code Finishes Executing (161253)
The information in this article applies to:
- Microsoft Access for Windows 95 7.0
- Microsoft Access 97
This article was previously published under Q161253 Moderate: Requires basic macro, coding, and interoperability skills.
SYMPTOMS
When you use Automation to control another application, such as Microsoft
Word or Microsoft Excel, the application quits as soon as your procedure
finishes running.
This article assumes that you are familiar with Visual Basic for
Applications and with creating Microsoft Access applications using the
programming tools provided with Microsoft Access. For more information
about Visual Basic for Applications, please refer to your version of the
"Building Applications with Microsoft Access" manual.
CAUSE
The application quits because its object variable loses scope.
RESOLUTION
If you do not want the Automation server application to close when your
code finishes running, use one of the following methods.
Method 1
Declare the object variable in the Declarations section of your code
module:
- Open the sample database Northwind.mdb.
- Create a module and type the following line in the Declarations section:
Dim xlApp as Object
- Type the following procedure:
Sub LeaveXLOpen1()
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
End Sub
- To test this function, type the following line in the Debug window,
and then press ENTER:
Note that Microsoft Excel starts and remains visible after the procedure
stops running. Microsoft Excel quits automatically when you close your
database because that is when the object variable loses scope.
NOTE: If you declare your object variable in the Declarations section of a
form or report module, Microsoft Excel remains open only as long as the
form or report remains open.
Method 2
Declare the object variable as a Static variable at the procedure level:
- Open the sample database Northwind.mdb.
- Create a module and type the following procedure:
Sub LeaveXLOpen2()
Static xlApp as Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
End Sub
- To test this function, type the following line in the Debug window,
and then press ENTER:
Note that Microsoft Excel starts and remains visible after the procedure
stops running. Microsoft Excel quits automatically when you close your
database because that is when the object variable loses scope.
NOTE: If you declare the Static variable as part of a procedure in a form
or report module, Microsoft Excel remains open only as long as the form or
report remains open.
REFERENCES
For more information about the scope and lifetime of variables, search the
Help Index for variables, scope, or variables, lifetime.
For more information about using the CreateObject function, search the
Help Index for CreateObject function.
For more information about declaring Static variables, search the Help
Index for Static statement.
For information about using Microsoft Access as an Automation server,
please see the following article in the Microsoft Knowledge Base:
147816
ACC: Using Microsoft Access as an Automation Server
For more information about quitting Microsoft Excel after you run
Automation code, please see the following article in the Microsoft
Knowledge Base:
145770
ACC: Automation Does Not Close Microsoft Excel
Modification Type: | Minor | Last Reviewed: | 10/11/2006 |
---|
Keywords: | kbinterop kbprb kbProgramming KB161253 |
---|
|