Desperately trying to close Excel!!!! Gurus help needed

A

Atchoum

Hi,

I am trying to open Excel, perform certain operations then leave it open so
that the user can close it whenever he wants. The problem is that Excel will
remain open when I close it manually. After hours of testing, I am down to
this:
the line s = Range.FindNext(s) below is the one causing problem. If I
comment it out, when I close Excel manually, it does close completely. If I
remove the comment, then Excel will not close.
Anyone has a solution for this?

Desperately yours,

Atchoum


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
oExcel = CreateObject("Excel.Application")
oWorkbook = oExcel.Workbooks.Open(Application.StartupPath &
"\test.xls)
For i = 1 To oWorkbook.Sheets.Count
Range = oWorkbook.Sheets(i).UsedRange
s = Range.Find("[[C:", lookin:=-4163) 'xlValues
If Not s Is Nothing Then
sFirstAddress = s.Address
Do
s = Range.FindNext(s) ' <------- Line that makes
Excel remain open after manually closing it ---------->
Loop While Not s Is Nothing And s.Address <>
sFirstAddress
End If
Next i
oExcel.Visible = True
NAR(s)
NAR(oWorkbook)
oExcel = Nothing
NAR(oExcel)
GC.Collect()
End Sub

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub
 
J

Jorge

Hi Atchoum

Once you use NAR and close the form excel will be closed.
I think its because the form has still a reference to
excel.

Another approach that i use is to get the excel process
ID when you create it and kill it manualy using the
process class.

Kind Regards
Jorge
-----Original Message-----
Hi,

I am trying to open Excel, perform certain operations then leave it open so
that the user can close it whenever he wants. The problem is that Excel will
remain open when I close it manually. After hours of testing, I am down to
this:
the line s = Range.FindNext(s) below is the one causing problem. If I
comment it out, when I close Excel manually, it does close completely. If I
remove the comment, then Excel will not close.
Anyone has a solution for this?

Desperately yours,

Atchoum


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
oExcel = CreateObject("Excel.Application")
oWorkbook = oExcel.Workbooks.Open (Application.StartupPath &
"\test.xls)
For i = 1 To oWorkbook.Sheets.Count
Range = oWorkbook.Sheets(i).UsedRange
s = Range.Find("[[C:", lookin:=- 4163) 'xlValues
If Not s Is Nothing Then
sFirstAddress = s.Address
Do
s = Range.FindNext(s) ' <------- Line that makes
Excel remain open after manually closing it ---------->
Loop While Not s Is Nothing And
 
A

Atchoum

Once you use NAR and close the form excel will be closed.
I think its because the form has still a reference to
excel.
The code is in a module though.
Another approach that i use is to get the excel process
ID when you create it and kill it manualy using the
process class.

How do you do that?

TIA
 
J

Jorge

-----Original Message-----

The code is in a module though.

I have mine in a class and it had the same problem.
How do you do that?

Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook = xlApp.Workbooks.Add
Dim xlSheet As Excel.Worksheet = DirectCast
(xlApp.ActiveSheet, Excel.Worksheet)

Dim y As Integer
Dim currentProcess As Process = Process.GetCurrentProcess
()
Dim myExcelPID As Integer

'' get a list of processes called excel
Dim localByName As Process() = Process.GetProcessesByName
("excel")


For y = 0 To localByName.GetUpperBound(0)
myExcelPID = localByName(y).Id
Exit For
Next

<code that uses excel snipped>

System.Runtime.InteropServices.Marshal.ReleaseComObject
(xlApp)
xlApp = Nothing

'' Verify if excel is still running
'' if so kill it

Dim aProcesses() As Process = Process.GetProcesses
Dim aProcess As Process

For y = 0 To aProcesses.GetUpperBound(0)
If aProcesses(y).Id = myExcelPID Then
aProcess = aProcesses(y)
Try
aProcess.Kill()
Catch ex As Exception

End Try
Exit For
End If
Next

GC.Collect()
 

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