472,806 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,806 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 2409
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{

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.