Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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.

Background

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)
    Else
        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.

License

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

Share

About the Author

Ganesh_IT
Web Developer
Unknown
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.

Comments and Discussions

 
GeneralAlternative to Excel Automation PinmemberBarbaMariolino23-Jun-08 2:01 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150123.1 | Last Updated 26 Sep 2007
Article Copyright 2007 by Ganesh_IT
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid