Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

How to stop the excel process created in my code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DAXU - 06 Feb 2008 20:54 GMT
Hello,
I did following code to open an excel sheet and now just close it. But
the problem is that the excel process created in my code is still
there when I use:
                   excelApp.Quit();
                   Marshal.ReleaseComObject(excelApp);
What else should I do to properly shutdown the excel process?

many Thanks.

Here is my code:
Microsoft.Office.Interop.Excel.Application excelApp=null;
           Microsoft.Office.Interop.Excel.Workbook newWorkbook =
null;
           Microsoft.Office.Interop.Excel.Sheets excelSheets = null;
           Microsoft.Office.Interop.Excel.Worksheet sheet = null;
           try
           {
               excelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
               //openExcelDoc.InitialDirectory =
string.Format(@"{0}Carweb", drives[0]);

               excelApp.Visible = false;
               newWorkbook =
excelApp.Workbooks.Open(templateExcelPath, 0, false, 5, "", "", false,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false,
0, true, false, false);
               excelSheets = newWorkbook.Worksheets;
               foreach (Microsoft.Office.Interop.Excel.Worksheet
excelWorksheet in excelSheets)
               {
                   string name = excelWorksheet.CodeName;
                   name = excelWorksheet.Name;
               }
           }
           finally
           {
               if (sheet != null)
               {

                   Marshal.ReleaseComObject(sheet);
               }
               if (excelSheets != null)
               {
                   Marshal.ReleaseComObject(excelSheets);
               }
               if (newWorkbook != null)
               {
                   newWorkbook.Close(Missing.Value, Missing.Value,
Missing.Value);
                   Marshal.ReleaseComObject(newWorkbook);
               }
               if (excelApp != null)
               {
                   excelApp.Quit();
                   Marshal.ReleaseComObject(excelApp);
               }
Nicholas Paldino [.NET/C# MVP] - 06 Feb 2008 21:20 GMT
DAXU,

   Well, you are leaking references which keep the Application instance
alive.  Quit doesn't kill the process until the reference count on the
Application instance is 0.

   Because you can get to the Application object from almost any object in
Excel, every object that you expose that you don't release, you add another
reference to the Application.

   From what it looks like, you are calling the Workbooks property and not
releasing the reference that you expose as a result of calling the property.

   You might want to look at this thread:

http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_t
hread/thread/bd8b992287ac935f/1d7fb35d82ce3627?lnk=st&q=nicholas+paldino+ComRefe
rence#1d7fb35d82ce3627


   In it, there is a structure I provide which will help clean up code that
properly releases COM references in a method scope.  The problem with your
code is that if an exception happens in the finally clause, you could end up
not releasing the other objects, and the app will continue to hang around.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hello,
> I did following code to open an excel sheet and now just close it. But
[quoted text clipped - 54 lines]
>                    Marshal.ReleaseComObject(excelApp);
>                }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.