By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.ReleaseComO bject(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.ReleaseComO bject(wkBook)
wkBook = Nothing

xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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

Apr 13 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <Ch***********@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
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.ReleaseComO bject(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.ReleaseComO bject(wkBook)
wkBook = Nothing

xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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

Apr 14 '06 #2

P: n/a
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" <Ch***********@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
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.ReleaseComO bject(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.ReleaseComO bject(wkBook)
wkBook = Nothing

xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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


Apr 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.