Closing current instance of Excel

G

Guest

I have a situation where I have more than 1 instance of Excel open. The most
recent instance is opened by code in my database. When the code line (see
below) that closes Excel is executed all instances close. I only want the
one that Access opened to close.

Any help would be appreciated.

DoCmd.RunMacro "mac:DelWalTB"
Set xlWkb = GetObject("g:\Accounting\AR\AR Database\Macros.xls")
Set xlApp = xlWkb.Parent

xlApp.Visible = True
xlWkb.Windows(1).Visible = True

xlWkb.Application.Run ("Import_TB")

xlWkb.Close savechanges:=False
Set xlWkb = Nothing
xlApp.Quit
Set xlApp = Nothing
 
D

Dave Patrick

All air code but try something like this to use the running process (if it
exists).

Function IsExecuting(sProc)
set list = getobject("winmgmts:").execquery(_
"select * from win32_process where name='" & sProc & "'")
If list.count > 0 Then IsExecuting = True
End Function

If Not IsExecuting("excel.exe") Then
Set xlApp = CreateObject("Excel.Application")
Else
Set xlApp = GetObject(, "Excel.Application")
End If

--
Regards,

Dave Patrick ....Please no email replies - reply in newsgroup.
Microsoft Certified Professional
Microsoft MVP [Windows]
http://www.microsoft.com/protect

:
|I have a situation where I have more than 1 instance of Excel open. The
most
| recent instance is opened by code in my database. When the code line (see
| below) that closes Excel is executed all instances close. I only want the
| one that Access opened to close.
|
| Any help would be appreciated.
|
| DoCmd.RunMacro "mac:DelWalTB"
| Set xlWkb = GetObject("g:\Accounting\AR\AR Database\Macros.xls")
| Set xlApp = xlWkb.Parent
|
| xlApp.Visible = True
| xlWkb.Windows(1).Visible = True
|
| xlWkb.Application.Run ("Import_TB")
|
| xlWkb.Close savechanges:=False
| Set xlWkb = Nothing
| xlApp.Quit
| Set xlApp = Nothing
|
 
G

Guest

The following code samples will get what you want. In Example One, you need
to defer error trapping. This code will only start an instance of Excel if
it is not already running.
********** Example One ***************
On Error Resume Next ' Defer error trapping.
Me.txtStatus = "Opening Spreadsheet"
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
blnExcelWasNotRunning = True
Set xlApp = CreateObject("excel.application")
Else
DetectExcel
End If
Err.Clear ' Clear Err object in case error occurred.
On Error GoTo LoadAdjustedActuals_Err
********** Example One ***************

Example Two is a sub called in Example One

********** Example Two ***************
Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim Hwnd As Long
' If Excel is running this API call returns its handle.
Hwnd = FindWindow("XLMAIN", 0)
If Hwnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage Hwnd, WM_USER + 18, 0, 0
End If
End Sub
********** Example Two ***************

Example Three shows how you decide whether to Quit Excel depending on
whether it was running or not
********** Example Three *************
If blnExcelWasNotRunning = True Then
xlApp.Quit
End If
Set xlApp = Nothing
********** Example Three *************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top