views:

136

answers:

1

Hi everyone

I've read many post looking for my answer, but all are similar to this: http://stackoverflow.com/questions/1610743/reading-excel-files-in-vb-net-leaves-excel-process-hanging

My problem is that I don't quit the app...

The idea is this:

If a User has Excel Open, if he has the file I'm interested in open... get that Excel instance and do whatever I want to do...

But I don't to close his File after I'm done... I want him to keep working on it, the problem is that when he closes Excel... The process keeps running... and running... and running after the user closes Excel with the X button...

this is how I try to do it

This piece is used to know if he has Excel open, and in the For I check for the file name I'm interested in.

    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 he hasn't Excel open, 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 opened 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 the user to keep the file open (not calling Quit()), Excel process keeps running after the user closes Excel with the X button.

Is there any way to do what I try to do?? Control a open instance of excel and releasing everything so when the user closes it with the X button, the Excel Process dies normally???

Thanks!!!

A: 

It seems to me it would be better to use CreateObject instead of GetObject(, "Excel.Application") (see http://support.microsoft.com/kb/288902/en and http://technet.microsoft.com/en-us/library/ee176980.aspx). Then call of Quit method before calling of FinalReleaseComObject and assigninh Nothing will be have a clear sense.

Oleg
But CreateObject creates a new Excel Instance... I want to get an already open Excel Book... and also, I want to leave it Open when my App finishes using it... the problem is that at the end, if the user closes Excel, the process keeps running
figus
If Excel already running you should not close it. If you use only GetObject(, "Excel.Application") you will be not able to start Excel if it is not yet running. Please read two references which I included in my answer. One more remark. It you start a new instance of an application it takes not double memory. Most of DLLs and the code of EXE will be shared between applications. Just compare "Memory - Working Set" with "Memory - Private Working Set" of an application. The names "Memory - ..." are the name of columns in Task Manager (for Windows 7) or use Process Explorer (www.sysinternals.com)
Oleg