Click here to Skip to main content
12,885,022 members (33,417 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 26 Sep 2007

Dealing with Excel objects

, 26 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
This article outlines the issue faced by most developers when it comes to dealing with Excel objects. Here, you will find code that will help in getting rid of Excel objects from the memory once they are no longer required.


This article focuses on a small piece of code that actually simulates the EndTask function. By using this, we can force end an EXCEL.EXE process once we are done using the object.


Excel objects created using MS Office Excel 2003 and below versions are basically COM objects. When we instantiate an object from code, it creates an EXCEL.EXE process in the system processes. This corresponds to a single instance of Excel. The .NET CLR is not responsible for releasing COM objects. All it can do is reduce the reference count of COM objects, which would then be cleared off by the GC, which is again not under the control of the developer as the GC could run at any instant. So there is no means of releasing an Excel object from the memory immediately. Once there are many EXCEL.EXE appearing in the Task Manager, it may end up in lack of memory errors.

Using the code

We would use the EndTask method of user32.dll to simulate the end task functionality which will release the EXCEL.EXE from the memory (Task Manager).

You need to import the EndTask method as shown below:

//method import
Private Declare Function EndTask Lib "user32.dll" Alias _
                "EndTask" (ByVal hwnd As Long) As Long
    //Getting the window handle for the excel
    If (objExcel.Version) < "10.0" Then
        iHandle = FindWindow(Nothing, ObjExcel.Caption)
        iHandle = objExcel.Parent.Hwnd
    End If

    //Calling the EndTask Method
    intResult = EndTask(iHandle)

Points of Interest

You would not require any special permissions to access the EndTask method. So if you can create the Excel object, you can very easily destroy it using this piece of code.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Developer with a Financial Company with more than 3 years of IT experience. Mainly working with .NET Technologies. Also have over 3 years experience with .NET 1.1 and 2.0, MS SQL 2000/2005, DB2 UDB and Office automation.

You may also be interested in...

Comments and Discussions

GeneralAlternative to Excel Automation Pin
BarbaMariolino23-Jun-08 1:01
memberBarbaMariolino23-Jun-08 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 26 Sep 2007
Article Copyright 2007 by Ganesh_IT
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid