PC Review


Reply
Thread Tools Rate Thread

not quitting excel

 
 
Mike
Guest
Posts: n/a
 
      16th Dec 2003
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
 
Reply With Quote
 
 
 
 
Morten Overgaard
Guest
Posts: n/a
 
      16th Dec 2003
have you tried
System.Runtime.InteropServices.Marshal.ReleaseComObject( excelObj );

after the quit method
This ought to do it

/morten

"Mike" <(E-Mail Removed)> wrote in message
news:016801c3c406$c58743d0$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      17th Dec 2003
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

>-----Original Message-----
>have you tried
>System.Runtime.InteropServices.Marshal.ReleaseComObject(

excelObj );
>
>after the quit method
>This ought to do it
>
>/morten
>
>"Mike" <(E-Mail Removed)> wrote in

message
>news:016801c3c406$c58743d0$(E-Mail Removed)...
>> 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

>
>
>.
>

 
Reply With Quote
 
Paul Clement
Guest
Posts: n/a
 
      17th Dec 2003
On Tue, 16 Dec 2003 16:58:01 -0800, <(E-Mail Removed)> 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.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 ~~~ http://www.pcreview.co.uk/forums/(E-Mail Removed)
Microsoft MVP (Visual Basic)
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      29th Dec 2003
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-----
>On Tue, 16 Dec 2003 16:58:01 -0800,

<(E-Mail Removed)> 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.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 Removed)
>Microsoft MVP (Visual Basic)
>.
>

 
Reply With Quote
 
Paul Clement
Guest
Posts: n/a
 
      30th Dec 2003
On Mon, 29 Dec 2003 08:00:21 -0800, "Mike" <(E-Mail Removed)> 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.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/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 ~~~ (E-Mail Removed)
Microsoft MVP (Visual Basic)
 
Reply With Quote
 
Ron Allen
Guest
Posts: n/a
 
      30th Dec 2003
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" <(E-Mail Removed)> wrote in message
news:1e1801c3ce24$dcb2b670$(E-Mail Removed)...
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-----
>On Tue, 16 Dec 2003 16:58:01 -0800,

<(E-Mail Removed)> 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.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 Removed)
>Microsoft MVP (Visual Basic)
>.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dot not app not quitting John Microsoft Dot NET Framework 4 2nd Jul 2007 07:33 AM
dot not app not quitting John Microsoft Dot NET 4 2nd Jul 2007 07:33 AM
EXCEL.EXE Process Won't Die After Quitting Excel (Seems MS Query Related) Lisa B. Microsoft Excel Discussion 0 29th Nov 2005 02:36 PM
Excel not quitting - How to avoid global reference? deko Microsoft Excel Programming 5 9th Mar 2005 10:30 PM
Closing Excel files from Access and/or quitting Excel from Access Beverly Microsoft Access VBA Modules 1 11th Oct 2003 06:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:46 PM.