[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to close Excel 2003 object through ASP.NET?

Posted on 2005-05-05
11
Medium Priority
?
222 Views
Last Modified: 2010-04-07
Hi Friends,

I am using the following piece of code to write an Excel file through ASP.NET and VB.NET.

            Dim objxlsAppln as Object
            Dim objxlsWorkSheet as Object
            objxlsAppln = CreateObject("Excel.Application")
            objxlsAppln.Workbooks.Open(Page.Server.MapPath("..\Reports\Template.xls"))
            objxlsWorkSheet = objxlsAppln.ActiveWorkbook.ActiveSheet
            .
            .
            .(Code to writing Excel File)
            .
            .
            'Code to save file and close excel object
            objxlsWorkSheet.SaveAs(Page.Server.MapPath("..\Reports\Report.xls"))
            objxlsAppln.ActiveWorkbook.Close()
            objxlsAppln.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(objxlsAppln)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(objxlsWorkSheet)
            GC.Collect()
            GC.WaitForPendingFinalizers()
            objxlsWorkSheet = Nothing
            objxlsAppln = Nothing

My problem is even after the above code is executed the excel.exe process is not closed. This runs well in Excel prior to version 2003, but in Excel 2003 it is not closing the excel.exe process.

Please help.

Thanks,
Nitin
0
Comment
Question by:nitinkpr
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 12

Expert Comment

by:fruhj
ID: 13942653
Hi nitinkpr,
  I looked at the help briefly
the only thing I came up with is to try the application.DisplayAlerts = false statement before you call application.quit -
Maybe something is coming up that you're not seeing?
My hunch is that it works fine with 2003 - and that there's something specific to your install thats causing it.
- Jack
0
 

Author Comment

by:nitinkpr
ID: 13942736
Hi fruhj,
i tried the the suggestion that u had given, but still problem remains the same.

-Nitin
0
 
LVL 12

Expert Comment

by:fruhj
ID: 13942882
When you run this code, does excel come up on the screen?

(I can't remember the default behaviour, I think it's hidden)

There's should be an application.visible property you can set to true.

If you haven't already, try doing that right after you do your create object("excel.application") so you can watch excel through the process - maybe you'll find something.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Expert Comment

by:Ramesh Srinivas
ID: 13943317
I use this and it works without problem....

'Create
Dim oXL As Excel.Application
oXL = CreateObject("Excel.Application")

'Close & Quit
oXL.Workbooks.Close()
oXL.Quit()
oXL = Nothing

Try declaring it as an excel app instead of an object. If that doesnt work then it might be your excel version - can you let us know which version it is please.

regards,

KS
0
 

Author Comment

by:nitinkpr
ID: 13943391
Thanx for ur replies.

I have declared as an Object because I want to use late binding without including Excel in project references and without using its interop version.

I am using Excel 2003 and the code snippet that I have given above runs well with version 2002 and prior.

regards,
Nitin
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13953594
i have read and seen this problem and most of time .. no definite solution is found .. u can search the net and u will see lots of unanswered threads on the same issue .. anyway, u would try this
set all the objects to nothing in the following order (based on which u have used)
range, worsheet, workbooks, excel application .. also remeber to use the ReleaseComObject (the way u have done it) for all these objects .. except the range .. again in the same order .. i had read somewhere that this problem also arises when one of the objects used is not released ... give this a try ..
0
 
LVL 26

Accepted Solution

by:
Rejojohny earned 200 total points
ID: 13953612
I forgot to mention . .close all the workbooks before releasing the memory ..

have a look at these links .. seems some of them were able to solve the problem
http://www.experts-exchange.com/Programming/Q_21228470.html

here the solution says about not using "with .. end with" statement ..
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21288000.html

here the solution says about using a loop to release the objects
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20504354.html

again a loop ... last comment by Tad and lecky
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20873630.html
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 14128222
I think I have covered all the options in the links provided ...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Integration Management Part 2
Loops Section Overview
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question