473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Re: Excel object cannot released without error

Many methods return objects when they are called. With Excel these objects
are placed in memory and must be destroyed via ReleaseComObject as you have
done with your NAR method, but the line:

objExcel.Workbooks.Open(Page.MapPath("reports\BLCo st.xls"))

could be the culprit since the Open method returns a reference to a Workbook
object that you haven't assigned a variable to. Since you have no variable
to explicitly use to destroy the reference, the reference counter for that
object can't get to zero and therefore Excel won't close.

You should always explicitly assign a variable to all references, that means
any method or property call that returns an object.

-Scott

"Raymond Chiu" <rc******@yahoo.com.hkwrote in message
news:ed**************@TK2MSFTNGP04.phx.gbl...
Dear All,
I have the problem which exists for several months and cannot find the
solution in the web, I see others also have the same situation but they
are also not solved.

It is excel object which still remain in the process in the task manager
in the server. Another project does not have this problem and they are on
the same server. I have tried to change from win2000 server to win2003
server, from ms office 2003 to ms office 2007. Excel still remain in the
process in the task manager. Although it would not affect another excel
running of another records, I need to clear the excel session everyday in
the server.

You can try using the attached excel file to see whether it would cause
the same problem in your computer. Please Let me know and your ideas.
Appreciate for your help!!!!

---------------------coding -----------------------------------------------------------------------------------

Dim objExcel As New Excel.Application
Dim objSheet As Excel.Worksheet

objExcel.Workbooks.Open(Page.MapPath("reports\BLCo st.xls"))

objExcel.DisplayAlerts = False
objSheet = objExcel.Sheets("Form")
objSheet.Copy(After:=objSheet)

objSheet = objExcel.ActiveSheet

.................................................. .....

objExcel.Sheets("Form").Delete()

objSheet.SaveAs(Page.MapPath("..\out\" & sFileName))

NAR(objSheet)
objExcel.Workbooks.Close()
NAR(objExcel.Workbooks)
objExcel.Quit()
NAR(objExcel)
GC.Collect()

---------------end of
sub-----------------------------------------------------------------

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)

Catch ex As Exception
Dim remarklabel As Label
remarklabel = CType(FormView1.FindControl("remarklabel"),
Label)
remarklabel.Text = remarklabel.Text + ex.ToString()
Finally
o = Nothing
End Try
End Sub

Jul 9 '08 #1
1 1355
Dear Scotte,

Then How can I do?
I have tried to the following.But still no work.
Is there anything I miss. If not using this way to open excel, is there any
other way to open?

dim objwb as excel.workbook
objwb=excel.workbooks.open(path)
then add the following after worksheet and before excel.

objwb.close
releasecom(objwb)
objwb=nothing
"Scott M." <s-***@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Many methods return objects when they are called. With Excel these objects
are placed in memory and must be destroyed via ReleaseComObject as you
have done with your NAR method, but the line:

objExcel.Workbooks.Open(Page.MapPath("reports\BLCo st.xls"))

could be the culprit since the Open method returns a reference to a
Workbook object that you haven't assigned a variable to. Since you have
no variable to explicitly use to destroy the reference, the reference
counter for that object can't get to zero and therefore Excel won't close.

You should always explicitly assign a variable to all references, that
means any method or property call that returns an object.

-Scott

"Raymond Chiu" <rc******@yahoo.com.hkwrote in message
news:ed**************@TK2MSFTNGP04.phx.gbl...
>Dear All,
I have the problem which exists for several months and cannot find the
solution in the web, I see others also have the same situation but they
are also not solved.

It is excel object which still remain in the process in the task manager
in the server. Another project does not have this problem and they are
on the same server. I have tried to change from win2000 server to win2003
server, from ms office 2003 to ms office 2007. Excel still remain in the
process in the task manager. Although it would not affect another excel
running of another records, I need to clear the excel session everyday in
the server.

You can try using the attached excel file to see whether it would cause
the same problem in your computer. Please Let me know and your ideas.
Appreciate for your help!!!!

---------------------coding -----------------------------------------------------------------------------------

Dim objExcel As New Excel.Application
Dim objSheet As Excel.Worksheet

objExcel.Workbooks.Open(Page.MapPath("reports\BLCo st.xls"))

objExcel.DisplayAlerts = False
objSheet = objExcel.Sheets("Form")
objSheet.Copy(After:=objSheet)

objSheet = objExcel.ActiveSheet

................................................. ......

objExcel.Sheets("Form").Delete()

objSheet.SaveAs(Page.MapPath("..\out\" & sFileName))

NAR(objSheet)
objExcel.Workbooks.Close()
NAR(objExcel.Workbooks)
objExcel.Quit()
NAR(objExcel)
GC.Collect()

---------------end of
sub-----------------------------------------------------------------

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)

Catch ex As Exception
Dim remarklabel As Label
remarklabel = CType(FormView1.FindControl("remarklabel"),
Label)
remarklabel.Text = remarklabel.Text + ex.ToString()
Finally
o = Nothing
End Try
End Sub


Jul 9 '08 #2

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

Similar topics

3
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...
4
by: Rich Wallace | last post by:
Hi all, I have a VB app that runs and manages individual XLS files within a single COM object. Upon processing the final fie, I attempt to close out the EXCEL object and release it using...
2
by: LBT | last post by:
Good day VB.Net seniors and experts, I have a window service application which will import data from Excel file. I face a problem where Excel.exe process cannot be released from memory once...
0
by: pshipway9[snipATsnip]yahoo.com | last post by:
I can't get vb.net to create a new Excel.Application object. MS Development Environment 7.1.3088 Net Framework 1.1 Office 2003 I understand the DIM exl as new Excel.Application stuff. I think...
4
by: Mr.Doubt | last post by:
I've a widows application, that makes use of an EXCEL COM object, to run a few macros available in a EXCEL file. The object is created when the app is started and is closed when the app is closed....
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
13
by: chuckie_9497 | last post by:
hello all you gurus. I am struggling with releasing com objects. I have isolated the problem to the code below. Objects are released and the process ends until I use "int k = sheet.Count;" Then...
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
7
by: Raymond Chiu | last post by:
Dear all, I have written the following code in the button click event but find that excel session object always remain in the server as seen from taskmanager. Do you know why?...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...

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.