Closing Excel Instance

G

Guest

In a module, I open an excel instance, do some operations, then close the
excel instance. At least, that's the intent of the module but I cannot get
the excel instance to close. Below are the relevant pieces of code. I would
like to know if I reference the objects correctly and if this is the source
of the problem. Also, I know I shouldn't use ActiveWorkbook, etc., but I am
the only user of the db so I figured I could manage it.

Dim objExcel As Excel.Application
Dim objWS As Excel.Worksheet
Dim objWB As Excel.Workbook

Set objExcel = GetObject(, "Excel.Application")

If Err.Number <> 0 Then
Err.Clear
Set objExcel = CreateObject("Excel.Application")
End If

Set objWB = objExcel.ActiveWorkbook
Set objWS = objWB.Sheets(1)

*** Code Here ****

objExcel.Quit
Set objWS = Nothing
Set objWB = Nothing
Set objExcel = Nothing
 
D

Dave Patrick

This article may help.

http://support.microsoft.com/default.aspx?scid=kb;[LN];187745

--
Regards,

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

:
| In a module, I open an excel instance, do some operations, then close the
| excel instance. At least, that's the intent of the module but I cannot
get
| the excel instance to close. Below are the relevant pieces of code. I
would
| like to know if I reference the objects correctly and if this is the
source
| of the problem. Also, I know I shouldn't use ActiveWorkbook, etc., but I
am
| the only user of the db so I figured I could manage it.
|
| Dim objExcel As Excel.Application
| Dim objWS As Excel.Worksheet
| Dim objWB As Excel.Workbook
|
| Set objExcel = GetObject(, "Excel.Application")
|
| If Err.Number <> 0 Then
| Err.Clear
| Set objExcel = CreateObject("Excel.Application")
| End If
|
| Set objWB = objExcel.ActiveWorkbook
| Set objWS = objWB.Sheets(1)
|
| *** Code Here ****
|
| objExcel.Quit
| Set objWS = Nothing
| Set objWB = Nothing
| Set objExcel = Nothing
|
|
 
G

Guest

Thx for the reply but that doesn't help. I forgot to mention that I am using
Office 2003.
 
D

Dave Patrick

See if this works for you.

Dim oExcel As Object, oSheet As Object
Dim running As Boolean
running = False
If IsExecuting("excel.exe") = True Then
Set oExcel = GetObject(, "Excel.Application")
running = True
Else
Set oExcel = CreateObject("Excel.Application")
End If
oExcel.Workbooks.Open "C:\Data\Excel\Coal_Plants_TPH_Graphs.xls"
'Do some stuff here
oExcel.ActiveWorkbook.Close True
If running = False Then
oExcel.Quit
End If
Set oSheet = Nothing
Set oExcel = Nothing

Public Function IsExecuting(sProc)
Dim list As Object
Set list = GetObject("winmgmts:").ExecQuery( _
"select * from win32_process where name='" & sProc & "'")
If list.Count > 0 Then IsExecuting = True
End Function



--
Regards,

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

:
| Thx for the reply but that doesn't help. I forgot to mention that I am
using
| Office 2003.
 

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

Similar Threads

can't get Excel to Quit 3
excel data into access 3
Excel stays in TaskManager 2
Excel automation, need little help !!! 3
Late Binding 5
Excel Bug in Excel 2007? Activewindow 0
VBA Excel File SaveAs 7
Saving current record 3

Top