473,404 Members | 2,187 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,404 software developers and data experts.

Help with vb.net controling excel and excel is hanging in mem

I have a vb.net app that is controling excel 2000 thru the com interop
interface. I have referenced the excel 9.0 library and have cut down the
code in the problem subroutine to this:

Dim objExcelApp As New Excel.Application

objExcelApp.Quit()
objExcelApp = Nothing

The problem is excel is hanging in memory and has to be closed by the task
manager. Anyone have any ideas why it is hanging.
Nov 20 '05 #1
5 2449
This is happening because the object that objExcelApp references has an
internal reference to Excel through COM automation and Excel will stay alive
as long as the object is alive. At first glance it would appear that this
would happen when you set objExcelApp to Nothing but that's not the case.
The object isn't destroyed at the point you set the variable to Nothing,
it's destroyed at some later point by the Garbage Collector thus extending
the life of Excel.

What you need to do is tell COM to release the internal connection between
your .NET object and the Excel COM object. You do this by adding one line of
code after the call to Quit as below:

Dim objExcelApp As New Excel.Application

objExcelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(objExcelApp)
objExcelApp = Nothing

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada

"David C. Allen" <da*****@avci.net> wrote in message
news:eL*************@tk2msftngp13.phx.gbl...
I have a vb.net app that is controling excel 2000 thru the com interop
interface. I have referenced the excel 9.0 library and have cut down the
code in the problem subroutine to this:

Dim objExcelApp As New Excel.Application

objExcelApp.Quit()
objExcelApp = Nothing

The problem is excel is hanging in memory and has to be closed by the task
manager. Anyone have any ideas why it is hanging.

Nov 20 '05 #2
* "David C. Allen" <da*****@avci.net> scripsit:
I have a vb.net app that is controling excel 2000 thru the com interop
interface. I have referenced the excel 9.0 library and have cut down the
code in the problem subroutine to this:

Dim objExcelApp As New Excel.Application

objExcelApp.Quit()
objExcelApp = Nothing

The problem is excel is hanging in memory and has to be closed by the task
manager. Anyone have any ideas why it is hanging.


PRB: Office Application Does Not Quit After Automation from Visual Studio .NET Client
<http://support.microsoft.com/?scid=kb;en-us;317109>

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Website Address Changed!
Nov 20 '05 #3
Hi David,

Check out Herfried's response for a link to a KB article. You might also
consider the following book:

Professional Visual Basic Interoperability - COM and VB6 to .NET
by Billy Hollis , Rockford Lhotka (WROX)

Rob
"David C. Allen" <da*****@avci.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
You are definately and MVP. That fixed my problem. Can you tell me where
that kind of information is documented?

Thanks!
"Rob Windsor [MVP]" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:O9**************@tk2msftngp13.phx.gbl...
This is happening because the object that objExcelApp references has an
internal reference to Excel through COM automation and Excel will stay alive
as long as the object is alive. At first glance it would appear that this
would happen when you set objExcelApp to Nothing but that's not the case. The object isn't destroyed at the point you set the variable to Nothing,
it's destroyed at some later point by the Garbage Collector thus extending the life of Excel.

What you need to do is tell COM to release the internal connection between your .NET object and the Excel COM object. You do this by adding one

line of
code after the call to Quit as below:

Dim objExcelApp As New Excel.Application

objExcelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(objExcelApp)
objExcelApp = Nothing

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada

"David C. Allen" <da*****@avci.net> wrote in message
news:eL*************@tk2msftngp13.phx.gbl...
I have a vb.net app that is controling excel 2000 thru the com interop
interface. I have referenced the excel 9.0 library and have cut down the code in the problem subroutine to this:

Dim objExcelApp As New Excel.Application

objExcelApp.Quit()
objExcelApp = Nothing

The problem is excel is hanging in memory and has to be closed by the

task manager. Anyone have any ideas why it is hanging.



Nov 20 '05 #4
Well it's doing it again. Not sure why. Here is the entire subroutine:

Sub ProcessExcelFile(ByVal lsReportPath As String, ByVal lsReportName As
String)

Dim objExcelApp As New Excel.Application

Dim SIMSWorkbookName As String

Try

lblStatus.Text = "Opening:" & lsReportPath & lsReportName

lblStatus.Refresh()

'open up the excel and the workbook

objExcelApp.Workbooks.Open(lsReportPath & lsReportName)

If chkShowExcel.Checked = True Then

objExcelApp.Visible = True

End If

objExcelApp.Workbooks(lsReportName).Unprotect("ops tab")

objExcelApp.DisplayAlerts = False

objExcelApp.Worksheets("SIMS RAW DATA").Visible = True

objExcelApp.Worksheets("SIMS RAW DATA").Activate()

objExcelApp.ActiveSheet.Cells.select()

objExcelApp.Selection.ClearContents()

SIMSWorkbookName = Path.GetFileName(msSIMSData)

objExcelApp.Workbooks.Open(msSIMSData)

objExcelApp.Workbooks(SIMSWorkbookName).Worksheets ("DowSIMSExtract").Activat
e()

objExcelApp.Workbooks(SIMSWorkbookName).Worksheets ("DowSIMSExtract").Cells.S
elect()

objExcelApp.Selection.Copy()

objExcelApp.Workbooks(lsReportName).Worksheets("SI MS RAW DATA").Activate()

objExcelApp.ActiveSheet.Range("A1").Select()

objExcelApp.ActiveSheet.Paste()

'objExcelApp.Run("ProcessSIMSData")

If chkPause.Checked = True And chkShowExcel.Checked = True Then

MessageBox.Show("Please review the chart and press ok when ready to close
chart file", gcAPPNAME, MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

objExcelApp.Workbooks(SIMSWorkbookName).Close(Fals e) 'don't save any changes

'Close down the workbook and excel

objExcelApp.Run("hidereports")

objExcelApp.ActiveWorkbook.Protect("opstab")

'objExcelApp.ActiveWorkbook.Save()

objExcelApp.Workbooks(lsReportName).Close(True) 'Close and save changes

'Close down Excel

objExcelApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComO bject(objExcelApp)

objExcelApp = Nothing

Catch ex As Exception

objExcelApp.Workbooks.Close()

objExcelApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComO bject(objExcelApp)

objExcelApp = Nothing

lblStatus.Text = "Error processing workbook name from: " & lsReportPath &
lsReportName & " Please check your task manager to ensure EXCEL is not hung
in memory by looking at the processes tab."

If gbBatchMode Then

WriteToEventLog("Error: " & Err.Description)

Else

MessageBox.Show("Error: " & Err.Description, gcAPPNAME,
MessageBoxButtons.OK, MessageBoxIcon.Warning)

End If

End Try

End Sub

"Rob Windsor [MVP]" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
Hi David,

Check out Herfried's response for a link to a KB article. You might also
consider the following book:

Professional Visual Basic Interoperability - COM and VB6 to .NET
by Billy Hollis , Rockford Lhotka (WROX)

Rob
"David C. Allen" <da*****@avci.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
You are definately and MVP. That fixed my problem. Can you tell me where
that kind of information is documented?

Thanks!
"Rob Windsor [MVP]" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:O9**************@tk2msftngp13.phx.gbl...
This is happening because the object that objExcelApp references has an internal reference to Excel through COM automation and Excel will stay

alive
as long as the object is alive. At first glance it would appear that this would happen when you set objExcelApp to Nothing but that's not the case. The object isn't destroyed at the point you set the variable to Nothing, it's destroyed at some later point by the Garbage Collector thus extending the life of Excel.

What you need to do is tell COM to release the internal connection between your .NET object and the Excel COM object. You do this by adding one line
of
code after the call to Quit as below:

Dim objExcelApp As New Excel.Application

objExcelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(objExcelApp)
objExcelApp = Nothing

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada

"David C. Allen" <da*****@avci.net> wrote in message
news:eL*************@tk2msftngp13.phx.gbl...
> I have a vb.net app that is controling excel 2000 thru the com interop > interface. I have referenced the excel 9.0 library and have cut down the > code in the problem subroutine to this:
>
> Dim objExcelApp As New Excel.Application
>
> objExcelApp.Quit()
> objExcelApp = Nothing
>
> The problem is excel is hanging in memory and has to be closed by

the task
> manager. Anyone have any ideas why it is hanging.
>
>



Nov 20 '05 #5
Ok I looked at the KB article. I put gc.collect at the end and it still had
a problem. Then I changed line:

objExcelApp.ActiveSheet.Cells.select()

to:

objExcelApp.Worksheets("SIMS RAW DATA").Cells.select()

and it was all better. Maybe I should do the same to the following lines
also just to be safe:

objExcelApp.ActiveSheet.Cells.select()
objExcelApp.Selection.ClearContents()
"David C. Allen" <da*****@avci.net> wrote in message
news:Of**************@TK2MSFTNGP12.phx.gbl...
Well it's doing it again. Not sure why. Here is the entire subroutine:

Sub ProcessExcelFile(ByVal lsReportPath As String, ByVal lsReportName As
String)

Dim objExcelApp As New Excel.Application

Dim SIMSWorkbookName As String

Try

lblStatus.Text = "Opening:" & lsReportPath & lsReportName

lblStatus.Refresh()

'open up the excel and the workbook

objExcelApp.Workbooks.Open(lsReportPath & lsReportName)

If chkShowExcel.Checked = True Then

objExcelApp.Visible = True

End If

objExcelApp.Workbooks(lsReportName).Unprotect("ops tab")

objExcelApp.DisplayAlerts = False

objExcelApp.Worksheets("SIMS RAW DATA").Visible = True

objExcelApp.Worksheets("SIMS RAW DATA").Activate()

objExcelApp.ActiveSheet.Cells.select()

objExcelApp.Selection.ClearContents()

SIMSWorkbookName = Path.GetFileName(msSIMSData)

objExcelApp.Workbooks.Open(msSIMSData)

objExcelApp.Workbooks(SIMSWorkbookName).Worksheets ("DowSIMSExtract").Activat e()

objExcelApp.Workbooks(SIMSWorkbookName).Worksheets ("DowSIMSExtract").Cells.S elect()

objExcelApp.Selection.Copy()

objExcelApp.Workbooks(lsReportName).Worksheets("SI MS RAW DATA").Activate()

objExcelApp.ActiveSheet.Range("A1").Select()

objExcelApp.ActiveSheet.Paste()

'objExcelApp.Run("ProcessSIMSData")

If chkPause.Checked = True And chkShowExcel.Checked = True Then

MessageBox.Show("Please review the chart and press ok when ready to close
chart file", gcAPPNAME, MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

objExcelApp.Workbooks(SIMSWorkbookName).Close(Fals e) 'don't save any changes
'Close down the workbook and excel

objExcelApp.Run("hidereports")

objExcelApp.ActiveWorkbook.Protect("opstab")

'objExcelApp.ActiveWorkbook.Save()

objExcelApp.Workbooks(lsReportName).Close(True) 'Close and save changes

'Close down Excel

objExcelApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComO bject(objExcelApp)

objExcelApp = Nothing

Catch ex As Exception

objExcelApp.Workbooks.Close()

objExcelApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComO bject(objExcelApp)

objExcelApp = Nothing

lblStatus.Text = "Error processing workbook name from: " & lsReportPath &
lsReportName & " Please check your task manager to ensure EXCEL is not hung in memory by looking at the processes tab."

If gbBatchMode Then

WriteToEventLog("Error: " & Err.Description)

Else

MessageBox.Show("Error: " & Err.Description, gcAPPNAME,
MessageBoxButtons.OK, MessageBoxIcon.Warning)

End If

End Try

End Sub

"Rob Windsor [MVP]" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
Hi David,

Check out Herfried's response for a link to a KB article. You might also
consider the following book:

Professional Visual Basic Interoperability - COM and VB6 to .NET
by Billy Hollis , Rockford Lhotka (WROX)

Rob
"David C. Allen" <da*****@avci.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
You are definately and MVP. That fixed my problem. Can you tell me where that kind of information is documented?

Thanks!
"Rob Windsor [MVP]" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message news:O9**************@tk2msftngp13.phx.gbl...
> This is happening because the object that objExcelApp references has an > internal reference to Excel through COM automation and Excel will stay alive
> as long as the object is alive. At first glance it would appear that

this
> would happen when you set objExcelApp to Nothing but that's not the

case.
> The object isn't destroyed at the point you set the variable to Nothing, > it's destroyed at some later point by the Garbage Collector thus

extending
> the life of Excel.
>
> What you need to do is tell COM to release the internal connection

between
> your .NET object and the Excel COM object. You do this by adding one

line
of
> code after the call to Quit as below:
>
> Dim objExcelApp As New Excel.Application
>
> objExcelApp.Quit()
> System.Runtime.InteropServices.Marshal.ReleaseComO bject(objExcelApp)
> objExcelApp = Nothing
>
> --
> Rob Windsor [MVP-VB]
> G6 Consulting
> Toronto, Canada
>
>
>
> "David C. Allen" <da*****@avci.net> wrote in message
> news:eL*************@tk2msftngp13.phx.gbl...
> > I have a vb.net app that is controling excel 2000 thru the com interop > > interface. I have referenced the excel 9.0 library and have cut
down
the
> > code in the problem subroutine to this:
> >
> > Dim objExcelApp As New Excel.Application
> >
> > objExcelApp.Quit()
> > objExcelApp = Nothing
> >
> > The problem is excel is hanging in memory and has to be closed by

the task
> > manager. Anyone have any ideas why it is hanging.
> >
> >
>
>



Nov 20 '05 #6

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

Similar topics

1
by: Richard Holliingsworth | last post by:
Hello: Thanks for your quick response. I'm trying to import a new Excel file into an A2K table and it's truncating the data. One of the Excel columns is a text field that can be up to 2000...
4
by: John Winterbottom | last post by:
This should be a fairly common scenario, but I haven't yet found a solution on google. (On WinXP Pro SP2 / Access 2003 / Excel 2003) The following code, when run from Access, leaves a copy of...
1
by: Michael Tkachev | last post by:
Hi Everybody, I created an Excel file in the ASP.Net. When I wrote this file on the disk I tryed to release COM objects. But I couldn't do it. When my method finished an "Excel" process exists...
3
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel...
15
by: John Machin | last post by:
I am pleased to announce a new general release (0.5.2) of xlrd, a Python package for extracting data from Microsoft Excel spreadsheets. CHANGES: * Book and sheet objects can now be pickled and...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
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...
1
by: =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?= | last post by:
Hello guys, I was wondering if anybody here had implemented a solution where Tables are aligned according to what the hanging indent tells us when there is a potential for the table overflowing...
8
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a web site that automates excel. This site runs in server 2003. iis 6.0 office 2003 installed I moved this app to server 2008 iis 7.0 office 2003 installed Now when I try to automate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.