not quitting excel

M

Mike

Please help. I am using object.quit in an attempt to
quit an excel application that I started with createobject
("Excel.Application"). The code executes, but does not
stop the application. When I run the exact same code for
Access and Word, it works fine (app closes).

Any ideas? Any help would be appreciated.

Thanks,
Mike Sweet
 
M

Morten Overgaard

have you tried
System.Runtime.InteropServices.Marshal.ReleaseComObject( excelObj );

after the quit method
This ought to do it

/morten
 
G

Guest

Morten,

Thank you for such a quick response. I tried your
suggestion and had no success. What's odd is that this
approach works if I replaced "excel.application"
with "word.application" or "access.application". I
wonder if this has something to do with my installation
of Excel. But what's also odd is that the quit method
works for Excel if I run it from an older version of vb.
It's just not working for vb .net 2003.

Below is the code. Would appreciate any insights.

Function validfile(ByVal in_file As String) As Boolean

Dim excel As Object

On Error Resume Next
validfile = True
excel = GetObject(, "excel.application")
If Err().Number <> 0 Then
excel = CreateObject("excel.application")
b_excelwasnotrunning = True
Err().Clear() ' Clear Err object in case
error occurred.
Else
b_excelwasnotrunning = False
End If
excel.Workbooks.Open(in_file)
If Err().Number <> 0 Then
validfile = False
Err().Clear()
End If
If b_excelwasnotrunning = True Then
excel.quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject
(excel)
excel = Nothing
End If


End Function

Thank you for any additional help.

Regards,
Mike Sweet
 
P

Paul Clement

¤ Morten,
¤
¤ Thank you for such a quick response. I tried your
¤ suggestion and had no success. What's odd is that this
¤ approach works if I replaced "excel.application"
¤ with "word.application" or "access.application". I
¤ wonder if this has something to do with my installation
¤ of Excel. But what's also odd is that the quit method
¤ works for Excel if I run it from an older version of vb.
¤ It's just not working for vb .net 2003.
¤
¤ Below is the code. Would appreciate any insights.
¤
¤ Function validfile(ByVal in_file As String) As Boolean
¤
¤ Dim excel As Object
¤
¤ On Error Resume Next
¤ validfile = True
¤ excel = GetObject(, "excel.application")
¤ If Err().Number <> 0 Then
¤ excel = CreateObject("excel.application")
¤ b_excelwasnotrunning = True
¤ Err().Clear() ' Clear Err object in case
¤ error occurred.
¤ Else
¤ b_excelwasnotrunning = False
¤ End If
¤ excel.Workbooks.Open(in_file)
¤ If Err().Number <> 0 Then
¤ validfile = False
¤ Err().Clear()
¤ End If
¤ If b_excelwasnotrunning = True Then
¤ excel.quit()
¤
¤ System.Runtime.InteropServices.Marshal.ReleaseComObject
¤ (excel)
¤ excel = Nothing
¤ End If
¤
¤
¤ End Function
¤
¤ Thank you for any additional help.
¤
¤ Regards,
¤ Mike Sweet

Try not to create implicit objects. For example, if you create a Workbook object by opening a
Workbook, set the returned object to a variable:

ExcelWorkbook = excel.Workbooks.Open(in_file)

ExcelWorkbook.Close, False

ExcelWorkbook = Nothing


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
M

Mike

Paul,

Thank you so much. The following code now does not work
for me though - it does not close Excel. However, if I
comment out the 3 excelworkbook lines, it does quit
Excel. Any thoughts:

excel = CreateObject("excel.application")
excelworkbook = excel.Workbooks.Open(in_file)
excelworkbook.close()
excelworkbook = Nothing
excel.quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject
(excel)
excel = Nothing
-----Original Message-----
¤ Morten,
¤
¤ Thank you for such a quick response. I tried your
¤ suggestion and had no success. What's odd is that this
¤ approach works if I replaced "excel.application"
¤ with "word.application" or "access.application". I
¤ wonder if this has something to do with my installation
¤ of Excel. But what's also odd is that the quit method
¤ works for Excel if I run it from an older version of vb.
¤ It's just not working for vb .net 2003.
¤
¤ Below is the code. Would appreciate any insights.
¤
¤ Function validfile(ByVal in_file As String) As Boolean
¤
¤ Dim excel As Object
¤
¤ On Error Resume Next
¤ validfile = True
¤ excel = GetObject(, "excel.application")
¤ If Err().Number <> 0 Then
¤ excel = CreateObject("excel.application")
¤ b_excelwasnotrunning = True
¤ Err().Clear() ' Clear Err object in case
¤ error occurred.
¤ Else
¤ b_excelwasnotrunning = False
¤ End If
¤ excel.Workbooks.Open(in_file)
¤ If Err().Number <> 0 Then
¤ validfile = False
¤ Err().Clear()
¤ End If
¤ If b_excelwasnotrunning = True Then
¤ excel.quit()
¤
¤ System.Runtime.InteropServices.Marshal.ReleaseComObject
¤ (excel)
¤ excel = Nothing
¤ End If
¤
¤
¤ End Function
¤
¤ Thank you for any additional help.
¤
¤ Regards,
¤ Mike Sweet

Try not to create implicit objects. For example, if you
create a Workbook object by opening a
 
P

Paul Clement

¤ Paul,
¤
¤ Thank you so much. The following code now does not work
¤ for me though - it does not close Excel. However, if I
¤ comment out the 3 excelworkbook lines, it does quit
¤ Excel. Any thoughts:
¤
¤ excel = CreateObject("excel.application")
¤ excelworkbook = excel.Workbooks.Open(in_file)
¤ excelworkbook.close()
¤ excelworkbook = Nothing
¤ excel.quit()
¤ System.Runtime.InteropServices.Marshal.ReleaseComObject
¤ (excel)
¤ excel = Nothing
¤

Which version of Excel are you using? If you are using XP or 2003 the primary interop assemblies
might be a better solution.

http://msdn.microsoft.com/library/d.../html/wrrefofficeprimaryinteropassemblies.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_oxppias.asp

As a last resort you can use the brute force method (API function calls) to terminate the Excel
process:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal
lpWindowName As String) As Int32
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg
As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

Public Function TerminateExcel()

Dim ClassName As String
Dim WindowHandle As Int32
Dim ReturnVal As Int32
Const WM_QUIT = &H12

Do

ClassName = "XLMain"
WindowHandle = FindWindow(ClassName, Nothing)

If WindowHandle Then
ReturnVal = PostMessage(WindowHandle, WM_QUIT, 0, 0)
End If

Loop Until WindowHandle = 0

End Function


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
R

Ron Allen

Mike,
You need to ReleaseComObject on your excel workbook and any other excel
objects that you create before setting them to nothing.
Ron Allen
Paul,

Thank you so much. The following code now does not work
for me though - it does not close Excel. However, if I
comment out the 3 excelworkbook lines, it does quit
Excel. Any thoughts:

excel = CreateObject("excel.application")
excelworkbook = excel.Workbooks.Open(in_file)
excelworkbook.close()
excelworkbook = Nothing
excel.quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject
(excel)
excel = Nothing
-----Original Message-----
¤ Morten,
¤
¤ Thank you for such a quick response. I tried your
¤ suggestion and had no success. What's odd is that this
¤ approach works if I replaced "excel.application"
¤ with "word.application" or "access.application". I
¤ wonder if this has something to do with my installation
¤ of Excel. But what's also odd is that the quit method
¤ works for Excel if I run it from an older version of vb.
¤ It's just not working for vb .net 2003.
¤
¤ Below is the code. Would appreciate any insights.
¤
¤ Function validfile(ByVal in_file As String) As Boolean
¤
¤ Dim excel As Object
¤
¤ On Error Resume Next
¤ validfile = True
¤ excel = GetObject(, "excel.application")
¤ If Err().Number <> 0 Then
¤ excel = CreateObject("excel.application")
¤ b_excelwasnotrunning = True
¤ Err().Clear() ' Clear Err object in case
¤ error occurred.
¤ Else
¤ b_excelwasnotrunning = False
¤ End If
¤ excel.Workbooks.Open(in_file)
¤ If Err().Number <> 0 Then
¤ validfile = False
¤ Err().Clear()
¤ End If
¤ If b_excelwasnotrunning = True Then
¤ excel.quit()
¤
¤ System.Runtime.InteropServices.Marshal.ReleaseComObject
¤ (excel)
¤ excel = Nothing
¤ End If
¤
¤
¤ End Function
¤
¤ Thank you for any additional help.
¤
¤ Regards,
¤ Mike Sweet

Try not to create implicit objects. For example, if you
create a Workbook object by opening a
 

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