ASP.NET: Can't Kill EXCEL.exe with Office PIA

Discussion in 'ASP .Net' started by =?Utf-8?B?Q2hyaXNGcm9obGljaA==?=, Apr 14, 2006.

  1. I have been trying to use the Office PIA's to write an ASP.NEt page to:
    1. Open a template workbook
    2. Populate some data
    3. Save the file back to the server
    4. Quit Excel and free up Memory

    I have been able to do all of the above steps except #4. Each time
    Excel.exe persists in memory. I have already referred to Q317109 "Office
    application does not quit after automation from Visual Studio .NET client"
    without much luck.

    I've tried stripping down the Office code to see where this hang is coming
    from exactly and found the following:

    WORKS: The following DID release Excel.exe:
    Dim xlApp As New Microsoft.Office.Interop.Excel.Application
    xlApp.Quit()
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
    xlApp = Nothing

    ERROR: When I augment it to the following however, Excel.exe continues to
    hang:
    Dim xlApp As New Microsoft.Office.Interop.Excel.Application
    Dim wkBook As Workbook = xlApp.Workbooks.Open("C:\test.xls")

    xlApp.Workbooks.Close()
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wkBook)
    wkBook = Nothing

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

    If anyone could explain why this might be happening, it would be greatly
    appreciated. It seems as if as soon as I get into working with a workbook,
    Excel.exe is stuck in memory. Rather than a specific fix for the above, I'm
    just looking to better understand the dynamics/rules here for avoiding this
    problem.

    Thanks in advance,

    Chris Frohlich
     
    =?Utf-8?B?Q2hyaXNGcm9obGljaA==?=, Apr 14, 2006
    #1
    1. Advertising

  2. your problem is the line.

    Dim wkBook As Workbook = xlApp.Workbooks.Open("C:\test.xls")

    because .net needs wrapper to call com objects it really turns into

    Dim wkBook As Workbook
    dim tmp as WorkBooks
    tmp = xlApp.Workbooks
    wkBook = tmp.Open("C:\test.xls")

    the tmp variable has a com ref that will not be released until its garabage
    collected. the rule you should use is never use 2 dots with com objects.
    alway create a tmp, and call ReleaseComObject() on the tmps.


    -- bruce (sqlwork.com)




    "ChrisFrohlich" <> wrote in message
    news:...
    >I have been trying to use the Office PIA's to write an ASP.NEt page to:
    > 1. Open a template workbook
    > 2. Populate some data
    > 3. Save the file back to the server
    > 4. Quit Excel and free up Memory
    >
    > I have been able to do all of the above steps except #4. Each time
    > Excel.exe persists in memory. I have already referred to Q317109 "Office
    > application does not quit after automation from Visual Studio .NET client"
    > without much luck.
    >
    > I've tried stripping down the Office code to see where this hang is coming
    > from exactly and found the following:
    >
    > WORKS: The following DID release Excel.exe:
    > Dim xlApp As New Microsoft.Office.Interop.Excel.Application
    > xlApp.Quit()
    > System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
    > xlApp = Nothing
    >
    > ERROR: When I augment it to the following however, Excel.exe continues to
    > hang:
    > Dim xlApp As New Microsoft.Office.Interop.Excel.Application
    > Dim wkBook As Workbook = xlApp.Workbooks.Open("C:\test.xls")
    >
    > xlApp.Workbooks.Close()
    > System.Runtime.InteropServices.Marshal.ReleaseComObject(wkBook)
    > wkBook = Nothing
    >
    > xlApp.Quit()
    > System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
    > xlApp = Nothing
    >
    > If anyone could explain why this might be happening, it would be greatly
    > appreciated. It seems as if as soon as I get into working with a
    > workbook,
    > Excel.exe is stuck in memory. Rather than a specific fix for the above,
    > I'm
    > just looking to better understand the dynamics/rules here for avoiding
    > this
    > problem.
    >
    > Thanks in advance,
    >
    > Chris Frohlich
    >
     
    bruce barker \(sqlwork.com\), Apr 14, 2006
    #2
    1. Advertising

  3. Besides thorough COM cleanup I still sometimes get stuck excel process so I
    implemented this routine.

    //kill EXCEL com processes
    System.Diagnostics.Process [] PROC = Process.GetProcessesByName("EXCEL");
    foreach(System.Diagnostics.Process PK in PROC)
    {//User excel process always have window name
    //COM process do not.
    if(PK.MainWindowTitle.Length==0)
    PK.Kill();
    }




    "bruce barker (sqlwork.com)" wrote:

    > your problem is the line.
    >
    > Dim wkBook As Workbook = xlApp.Workbooks.Open("C:\test.xls")
    >
    > because .net needs wrapper to call com objects it really turns into
    >
    > Dim wkBook As Workbook
    > dim tmp as WorkBooks
    > tmp = xlApp.Workbooks
    > wkBook = tmp.Open("C:\test.xls")
    >
    > the tmp variable has a com ref that will not be released until its garabage
    > collected. the rule you should use is never use 2 dots with com objects.
    > alway create a tmp, and call ReleaseComObject() on the tmps.
    >
    >
    > -- bruce (sqlwork.com)
    >
    >
    >
    >
    > "ChrisFrohlich" <> wrote in message
    > news:...
    > >I have been trying to use the Office PIA's to write an ASP.NEt page to:
    > > 1. Open a template workbook
    > > 2. Populate some data
    > > 3. Save the file back to the server
    > > 4. Quit Excel and free up Memory
    > >
    > > I have been able to do all of the above steps except #4. Each time
    > > Excel.exe persists in memory. I have already referred to Q317109 "Office
    > > application does not quit after automation from Visual Studio .NET client"
    > > without much luck.
    > >
    > > I've tried stripping down the Office code to see where this hang is coming
    > > from exactly and found the following:
    > >
    > > WORKS: The following DID release Excel.exe:
    > > Dim xlApp As New Microsoft.Office.Interop.Excel.Application
    > > xlApp.Quit()
    > > System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
    > > xlApp = Nothing
    > >
    > > ERROR: When I augment it to the following however, Excel.exe continues to
    > > hang:
    > > Dim xlApp As New Microsoft.Office.Interop.Excel.Application
    > > Dim wkBook As Workbook = xlApp.Workbooks.Open("C:\test.xls")
    > >
    > > xlApp.Workbooks.Close()
    > > System.Runtime.InteropServices.Marshal.ReleaseComObject(wkBook)
    > > wkBook = Nothing
    > >
    > > xlApp.Quit()
    > > System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
    > > xlApp = Nothing
    > >
    > > If anyone could explain why this might be happening, it would be greatly
    > > appreciated. It seems as if as soon as I get into working with a
    > > workbook,
    > > Excel.exe is stuck in memory. Rather than a specific fix for the above,
    > > I'm
    > > just looking to better understand the dynamics/rules here for avoiding
    > > this
    > > problem.
    > >
    > > Thanks in advance,
    > >
    > > Chris Frohlich
    > >

    >
    >
    >
     
    =?Utf-8?B?S2VpdGg=?=, Apr 25, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin Bruney

    Office PIA bomb out

    Alvin Bruney, Jul 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    451
    Alvin Bruney
    Jul 13, 2003
  2. =?Utf-8?B?Q2hyaXNGcm9obGljaA==?=

    Deploy Office PIA to ASP.NET Server

    =?Utf-8?B?Q2hyaXNGcm9obGljaA==?=, Apr 12, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    5,394
    David Browne
    Apr 12, 2006
  3. Stan Accrington
    Replies:
    1
    Views:
    986
    Michael Borgwardt
    May 13, 2004
  4. =?Utf-8?B?dGlnZXJqYWRl?=

    COM Error using Office 2003 PIA in ASP.Net

    =?Utf-8?B?dGlnZXJqYWRl?=, Jul 31, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    3,501
    =?Utf-8?B?dGlnZXJqYWRl?=
    Aug 1, 2006
  5. dinoo
    Replies:
    3
    Views:
    1,965
    Andrew Morton
    Jul 9, 2009
Loading...

Share This Page