469,643 Members | 1,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

not quitting excel

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
Jul 21 '05 #1
7 2289
have you tried
System.Runtime.InteropServices.Marshal.ReleaseComO bject( excelObj );

after the quit method
This ought to do it

/morten

"Mike" <mi**********@fidelis-partners.com> wrote in message
news:01****************************@phx.gbl...
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

Jul 21 '05 #2
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.ReleaseComO bject
(excel)
excel = Nothing
End If
End Function

Thank you for any additional help.

Regards,
Mike Sweet
-----Original Message-----
have you tried
System.Runtime.InteropServices.Marshal.ReleaseCom Object( excelObj );
after the quit method
This ought to do it

/morten

"Mike" <mi**********@fidelis-partners.com> wrote in messagenews:01****************************@phx.gbl...
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

.

Jul 21 '05 #3
On Tue, 16 Dec 2003 16:58:01 -0800, <an*******@discussions.microsoft.com> wrote:

¤ 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.ReleaseComO bject
¤ (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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #4
On Tue, 16 Dec 2003 16:58:01 -0800, <an*******@discussions.microsoft.com> wrote:

¤ 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.ReleaseComO bject
¤ (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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #5
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.ReleaseComO bject
(excel)
excel = Nothing
-----Original Message-----
On Tue, 16 Dec 2003 16:58:01 -0800, <an*******@discussions.microsoft.com> wrote:
¤ 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.ReleaseComO bject
¤ (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 aWorkbook, set the returned object to a variable:

ExcelWorkbook = excel.Workbooks.Open(in_file)

ExcelWorkbook.Close, False

ExcelWorkbook = Nothing
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
.

Jul 21 '05 #6
On Mon, 29 Dec 2003 08:00:21 -0800, "Mike" <an*******@discussions.microsoft.com> wrote:

¤ 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.ReleaseComO bject
¤ (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/de...assemblies.asp
http://msdn.microsoft.com/library/de...dc_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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #7
Mike,
You need to ReleaseComObject on your excel workbook and any other excel
objects that you create before setting them to nothing.
Ron Allen
"Mike" <an*******@discussions.microsoft.com> wrote in message
news:1e****************************@phx.gbl...
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.ReleaseComO bject
(excel)
excel = Nothing
-----Original Message-----
On Tue, 16 Dec 2003 16:58:01 -0800, <an*******@discussions.microsoft.com> wrote:
¤ 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.ReleaseComO bject
¤ (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 aWorkbook, set the returned object to a variable:

ExcelWorkbook = excel.Workbooks.Open(in_file)

ExcelWorkbook.Close, False

ExcelWorkbook = Nothing
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
.

Jul 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Deaconess | last post: by
8 posts views Thread by bearophileHUGS | last post: by
1 post views Thread by George | last post: by
6 posts views Thread by Mike | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.