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

A

Atchoum

Hi,

(Sorry if this is a repost but I don't see my original post in the
newsgroup).

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

(well reposting my answer)
Hi Atchoum

Once you use NAR excel will be closed when you close the
form that called it even though you used ReleaseComObject.
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 manually using the
process class.

Kind Regards
Jorge

-----Original Message-----
Hi,

(Sorry if this is a repost but I don't see my original post in the
newsgroup).

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
 

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