Freeing Excel after using Interop
I have read many posts looking for my answer, but they are all like this: Reading excel files in vb.net leaves the excel process stuck
My problem is that I am not logging out of the application ...
The idea is this:
If the user has Excel Open, if he has a file that I am interested in opening ... get an instance of Excel and do whatever I want to do ...
But I don't close his File after I am done ... I want him to keep working on this, the problem is when he closes Excel ... The process keeps running ... and running ... and running after the user closes Excel with the X ...
this is how i try to do it
This part is used to find out if Excel is open, and in the "I" field I check the file name that interests me.
Try
oApp = GetObject(, "Excel.Application")
libroAbierto = True
For Each libro As Microsoft.Office.Interop.Excel.Workbook In oApp.Workbooks
If libro.Name = EquipoASeccionIdSeccion.Text & ".xlsm" Then
Exit Try
End If
Next
libroAbierto = False
Catch ex As Exception
oApp = New Microsoft.Office.Interop.Excel.Application
End Try
here would be my code ... if it didn't open Excel, I create a new instance, open the file and everything else.
My code ends with this:
If Not libroAbierto Then
libroSeccion.Close(SaveChanges:=True)
oApp.Quit()
Else
oApp.UserControl = True
libroSeccion.Save()
End If
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(libroOriginal)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(libroSeccion)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(origen)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(copiada)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp)
libroOriginal = Nothing
libroSeccion = Nothing
oApp = Nothing
origen = Nothing
copiada = Nothing
nuevosGuardados = True
So you can see that if I open the file, I call oApp.Quit () and everything else, and the Excel process ends after a few seconds (maybe 5 aprox.)
BUT, if I mean that the user has to keep the file open (without calling Quit ()), the Excel process continues to run after the user closes Excel with the X button.
Is there a way to do what I am trying to do? Manage the open excel instance and freeing everything so that when the user closes it with the X button, the Excel process usually dies
Thanks!!!
a source to share
It seems to me better to use CreateObject
instead GetObject(, "Excel.Application")
(see http://support.microsoft.com/kb/288902/en and http://technet.microsoft.com/en-us/library/ee176980.aspx ). Then call the method Quit
before calling FinalReleaseComObject and assigninh. Nothing will make clear sense.
a source to share