473,655 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

COM Question (Excel)

Dear All,

I have create an excel (COM Object) using this code

Excel.Applicati on xls = new Excel.Applicati on();

but I can not remove it from memory although I have using close and quit

wb.Close(false, false, 0);
xls.Quit();
If I open task manager there is excel.exe. If I run multiple time the number
of excel.exe in task manager will increase.

Please Help.

Regards,
Gunawan
Feb 2 '07 #1
6 2065
In article <uB************ **@TK2MSFTNGP06 .phx.gbl>,
jg**********@gm ail.com says...
Dear All,

I have create an excel (COM Object) using this code

Excel.Applicati on xls = new Excel.Applicati on();

but I can not remove it from memory although I have using close and quit

wb.Close(false, false, 0);
xls.Quit();
If I open task manager there is excel.exe. If I run multiple time the number
of excel.exe in task manager will increase.
What is "wb"? A reference to a workbook I assume?

Make sure you release every COM object you get a reference to using
Marshal.Release ComObject()

Excel.Applicati on xls = new Excel.Applicati on();
try
{
wb = ...
try
{
...
}
finally
{
wb.Close(false, false, 0);
Marshal.Release ComObject(wb);
}
}
finally
{
xls.Quit();
Marshal.Release ComObject(xls);
}

--
Patrick Steele
http://weblogs.asp.net/psteele
Feb 2 '07 #2
Hi Patrick,
I still can not release excel.exe from the memory.

Currenty I am using this code on *.aspx file to response click button.

Is it related to this issue? and I am using visual studio 2003.
Marshal.Release ComObject still can not remove excel.exe. :(
Regards,
Gunawan
"Patrick Steele" <pa*****@mvps.o rgwrote in message
news:MP******** *************** *@msnews.micros oft.com...
In article <uB************ **@TK2MSFTNGP06 .phx.gbl>,
jg**********@gm ail.com says...
>Dear All,

I have create an excel (COM Object) using this code

Excel.Applicat ion xls = new Excel.Applicati on();

but I can not remove it from memory although I have using close and quit

wb.Close(false , false, 0);
xls.Quit();
If I open task manager there is excel.exe. If I run multiple time the
number
of excel.exe in task manager will increase.

What is "wb"? A reference to a workbook I assume?

Make sure you release every COM object you get a reference to using
Marshal.Release ComObject()

Excel.Applicati on xls = new Excel.Applicati on();
try
{
wb = ...
try
{
...
}
finally
{
wb.Close(false, false, 0);
Marshal.Release ComObject(wb);
}
}
finally
{
xls.Quit();
Marshal.Release ComObject(xls);
}

--
Patrick Steele
http://weblogs.asp.net/psteele

Feb 2 '07 #3
It's more likely that whatever you are doing in Excel is leaving something
open. Are you doing OLE Automation or what? This is a problem *many* people
have when doing OLE Automation with Excel.

For example, I had a problem once and it turned out to be a problem with
Ranges. If you use any kind of Ranges (like
..range(.cells( 1,2),.cells(3,4 )).merge=true), Excel creates a Range object
behind the scenes, and does not dispose of it. You have to do this:

(Sorry this is VB)

Dim myRange as Range = xlsheet.range(. Cells(1,2),.cel ls(3,4))
myRange.Merge=T rue
myRange = Nothing

That's just one example, but it depends on what you're doing. The way I
tracked it down was I commented out every single excel command I was using,
then uncommented them one at a time until I found the culprit. Tedious, but
effective.

So the question is, what are you doing with Excel? Are you just opening it
and closing it, or are you doing something in-between?

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
"Gunawan" <jg**********@g mail.comwrote in message
news:uw******** *****@TK2MSFTNG P06.phx.gbl...
Hi Patrick,
I still can not release excel.exe from the memory.

Currenty I am using this code on *.aspx file to response click button.

Is it related to this issue? and I am using visual studio 2003.
Marshal.Release ComObject still can not remove excel.exe. :(
Regards,
Gunawan
"Patrick Steele" <pa*****@mvps.o rgwrote in message
news:MP******** *************** *@msnews.micros oft.com...
>In article <uB************ **@TK2MSFTNGP06 .phx.gbl>,
jg**********@gm ail.com says...
>>Dear All,

I have create an excel (COM Object) using this code

Excel.Applica tion xls = new Excel.Applicati on();

but I can not remove it from memory although I have using close and
quit

wb.Close(fals e, false, 0);
xls.Quit();
If I open task manager there is excel.exe. If I run multiple time the
number
of excel.exe in task manager will increase.

What is "wb"? A reference to a workbook I assume?

Make sure you release every COM object you get a reference to using
Marshal.Releas eComObject()

Excel.Applicat ion xls = new Excel.Applicati on();
try
{
wb = ...
try
{
...
}
finally
{
wb.Close(false , false, 0);
Marshal.Releas eComObject(wb);
}
}
finally
{
xls.Quit();
Marshal.Releas eComObject(xls) ;
}

--
Patrick Steele
http://weblogs.asp.net/psteele


Feb 2 '07 #4
I will try to explain what I am doing with excel file.

I try to open excel file
Open Excel file
---------------
Application xls = new Application();
_Workbook wb = xls.Workbooks.O pen(strFilename ,
0, true, 5, "", "", true, Excel.XlPlatfor m.xlWindows,
"\t", false, false, 0 , true, 0, 0);

_Worksheet wsh = (_Worksheet) wb.Worksheets[1];

Retrieving Data
for
{

strKodeBarang = ((Range) wsh.Cells[i,1]).Value2.ToStri ng() ;
strNamaBarang = ((Range) wsh.Cells[i,2]).Value2.ToStri ng() ;
}
Releasing Object
-----------------
Marshal.Release ComObject(wsh);

wb.Close(false, false, 0);
Marshal.Release ComObject(wb);

xls.Quit();
Marshal.Release ComObject(xls);
I use this tehnique using vb6 and no excel.exe retain on memory.
But using C# always left excel.exe in memory.

Regards,
Gun

"RobinS" <Ro****@NoSpam. yah.nonewrote in message
news:HO******** *************** *******@comcast .com...
It's more likely that whatever you are doing in Excel is leaving something
open. Are you doing OLE Automation or what? This is a problem *many*
people have when doing OLE Automation with Excel.

For example, I had a problem once and it turned out to be a problem with
Ranges. If you use any kind of Ranges (like
.range(.cells(1 ,2),.cells(3,4) ).merge=true), Excel creates a Range object
behind the scenes, and does not dispose of it. You have to do this:

(Sorry this is VB)

Dim myRange as Range = xlsheet.range(. Cells(1,2),.cel ls(3,4))
myRange.Merge=T rue
myRange = Nothing

That's just one example, but it depends on what you're doing. The way I
tracked it down was I commented out every single excel command I was
using, then uncommented them one at a time until I found the culprit.
Tedious, but effective.

So the question is, what are you doing with Excel? Are you just opening it
and closing it, or are you doing something in-between?

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
"Gunawan" <jg**********@g mail.comwrote in message
news:uw******** *****@TK2MSFTNG P06.phx.gbl...
>Hi Patrick,
I still can not release excel.exe from the memory.

Currenty I am using this code on *.aspx file to response click button.

Is it related to this issue? and I am using visual studio 2003.
Marshal.Releas eComObject still can not remove excel.exe. :(
Regards,
Gunawan
"Patrick Steele" <pa*****@mvps.o rgwrote in message
news:MP******* *************** **@msnews.micro soft.com...
>>In article <uB************ **@TK2MSFTNGP06 .phx.gbl>,
jg**********@gm ail.com says...
Dear All,

I have create an excel (COM Object) using this code

Excel.Applic ation xls = new Excel.Applicati on();

but I can not remove it from memory although I have using close and
quit

wb.Close(fal se, false, 0);
xls.Quit() ;
If I open task manager there is excel.exe. If I run multiple time the
number
of excel.exe in task manager will increase.

What is "wb"? A reference to a workbook I assume?

Make sure you release every COM object you get a reference to using
Marshal.Relea seComObject()

Excel.Applica tion xls = new Excel.Applicati on();
try
{
wb = ...
try
{
...
}
finally
{
wb.Close(fals e, false, 0);
Marshal.Relea seComObject(wb) ;
}
}
finally
{
xls.Quit();
Marshal.Relea seComObject(xls );
}

--
Patrick Steele
http://weblogs.asp.net/psteele



Feb 2 '07 #5
Hi,

Gunawan wrote:
Hi Patrick,
I still can not release excel.exe from the memory.

Currenty I am using this code on *.aspx file to response click button.
That's quite a bad idea, regardless of all the other problems you're
getting:

http://support.microsoft.com/default...US;q257757#kb2

There are other ways to deal with Excel on the server. Using ADO.NET ist
a good way, or you can also manipulate Excel files in XML format.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Feb 2 '07 #6
"Gunawan" <jg**********@g mail.comwrote in message
news:eZ******** ******@TK2MSFTN GP03.phx.gbl...
>I will try to explain what I am doing with excel file.

I try to open excel file
Open Excel file
---------------
Application xls = new Application();
_Workbook wb = xls.Workbooks.O pen(strFilename ,
0, true, 5, "", "", true, Excel.XlPlatfor m.xlWindows,
"\t", false, false, 0 , true, 0, 0);

_Worksheet wsh = (_Worksheet) wb.Worksheets[1];

Retrieving Data
for
{

strKodeBarang = ((Range) wsh.Cells[i,1]).Value2.ToStri ng() ;
strNamaBarang = ((Range) wsh.Cells[i,2]).Value2.ToStri ng() ;
}
Releasing Object
-----------------
Marshal.Release ComObject(wsh);

wb.Close(false, false, 0);
Marshal.Release ComObject(wb);

Should be...
xls.Quit();
GC.Collect();
GC.WaitForPendi ngFinalizers();

Anyway, you should know that this is an unsupported scenario for Office application,
especially in .NET as you can never rely on the Finalizer to release all outstanding COM
references.

http://support.microsoft.com/kb/257757

Willy.

Feb 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
35524
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet and extract information from specific worksheets and cells. I'm not really sure how to get started with this process. I ran the COM Makepy utility from my PythonWin (IDE from ActiveSTate),
3
20275
by: Otie | last post by:
I found the following under the GetObject help notes and in the example for GetObject: "This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet (MyXL). It uses the worksheet's Application property to make Microsoft Excel visible, to close it, and so on. Using two API calls, the DetectExcel Sub procedure looks for Microsoft Excel, and if it is running, enters it in the Running Object Table. The...
6
12488
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C# application. It seems simple enough, but the problem I'm encountering is as follows: In order for the user to select the cell from Excel, they must first click once on the Excel window to give it focus and then their second click is what changes the cell...
14
5783
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the SQL DATABASE, probably by the click of a button. Is this possible? & what is the BEST APPROACH for doing this? & also if any links are there do tell those to me too coz I have no idea how to go about doing it.
22
15329
by: Howard Kaikow | last post by:
There's a significant problem in automating Excel from VB .NET. Reminds me of a problem I encountered almost 3 years ago that was caused by the Norton Auntie Virus Office plug-in. Can anybody reproduce the behavior described below? For this example, I am using Excel 2002 and VS .NET 2002 and VB 6. MSFT KB article 304661 gives a trivial example of early and late binding to Excel from VB .NET. Note that there is a variable naming...
9
2811
by: Anthony | last post by:
To me, creating Excel 2003 spreadsheets programmatically via VB.NET hasn't really changed since the days of VB6. That is, I'd do something similar to this Code: Dim ExcelApp As Excel.Application Dim ExcelWB As Excel.Workbook
7
2420
by: Alain \Mbuna\ | last post by:
Hi everybody. In my program I have some data that is calculated after some input from the user. I have written some code that opens an Excel workbook, with 5 worksheets and the calculated data (no database!)with some titles and info, is entered in the worksheet in a printable format. This is some of the code... Public exlAppl As Excel.Application
16
2689
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public System.Collections.Generic.List<frmMain.sDBTest> LoadTestSet(string TestSetFile, System.Collections.Generic.List<frmMain.sDBTestDBviewList)
9
4541
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso TypeOf obj Is IDisposable Then DirectCast(obj, IDisposable).Dispose()
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.