473,379 Members | 1,330 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,379 software developers and data experts.

Closing Excel

Hey,

I have some code where I open up Excel then loop through several cases to
update several workbooks.

Basically something like this (not showing all the code just basic structure).
Try
appExcel = New Excel Application
Do While Counter <=2
Select Case Counter
Case 1
eTemplate = "Some Excel File1"
RName = "New Version of File1"
Case 2
eTemplate = "Some Excel File2"
RName = "New Version of File2"
End select
wb = .Workbooks.Open(RTemplate)
With wb
.RefreshAll()
.SaveAs(RName)
.Close()
End With

'Increment Counter
Counter += 1

Loop
Catch ex As Exception
'Report Error
MessageBox.Show((ex.ToString))
Finally
If Not appExcel Is Nothing Then
GC.Collect()
GC.WaitForPendingFinalizers()

If Not wb Is Nothing Then
wb.Close(SaveChanges:=False)
Marshal.FinalReleaseComObject(fr.wb)
wb = Nothing
End If

appExcel.Quit()
Marshal.FinalReleaseComObject(fr.appExcel)
appExcel = Nothing
End If

End Try

I'm getting an error on the close statement in the Finally block. The error
states that the object doesn't exist (disconnected). Which makes sense if
everything executes correctly because I have a close statement prior to this
statement (in the loop code). But I'm not sure why I'm getting the error
because the if statement is checking for nothing.

My assumption is I need the early close statement so I can correctly loop
through and open then close the various excel files.

I also thought by checking in the Finally block if the wb object is nothing
would be good in case anything went wrong and a wb instance was left open. I
guess I don't understand why I'm getting this error when everything does
close properly because I would think the if statement in the finally block
would evaluate to nothing and not execute but instead I get an object
disconnected error.

Any help is greatly appreciated. I'm very new to VB.NET. Thanks

--
-ridawg

--
-ridawg
Sep 26 '06 #1
2 3386
Why do you close the workbook again in "Finally" block? the workbook getting
closed in "Try" block does not mean variable "wb" becomes Nothing, it still
points to the momory where workbook object was hosted there, although the
workbook object has been closed. Variable "wb" will hold that value until it
is out of scope, or you set it to Nothing. Thus, simply because a pointer is
not Nothing, it does not mean you can close an object twice the pointer
pointed to, if the said object has been closed previously. Therefore, you
got the error.

You might only need

Try
...
Catch
....
Finally

If Not appExcel Is Nothing Then

wb = Nothing
appExcel.Quit()
Marshal.FinalReleaseComObject(fr.appExcel)
appExcel = Nothing

End If
End Try
"ridawg" <ri****@discussions.microsoft.comwrote in message
news:46**********************************@microsof t.com...
Hey,

I have some code where I open up Excel then loop through several cases to
update several workbooks.

Basically something like this (not showing all the code just basic
structure).
Try
appExcel = New Excel Application
Do While Counter <=2
Select Case Counter
Case 1
eTemplate = "Some Excel File1"
RName = "New Version of File1"
Case 2
eTemplate = "Some Excel File2"
RName = "New Version of File2"
End select
wb = .Workbooks.Open(RTemplate)
With wb
.RefreshAll()
.SaveAs(RName)
.Close()
End With

'Increment Counter
Counter += 1

Loop
Catch ex As Exception
'Report Error
MessageBox.Show((ex.ToString))
Finally
If Not appExcel Is Nothing Then
GC.Collect()
GC.WaitForPendingFinalizers()

If Not wb Is Nothing Then
wb.Close(SaveChanges:=False)
Marshal.FinalReleaseComObject(fr.wb)
wb = Nothing
End If

appExcel.Quit()
Marshal.FinalReleaseComObject(fr.appExcel)
appExcel = Nothing
End If

End Try

I'm getting an error on the close statement in the Finally block. The
error
states that the object doesn't exist (disconnected). Which makes sense if
everything executes correctly because I have a close statement prior to
this
statement (in the loop code). But I'm not sure why I'm getting the error
because the if statement is checking for nothing.

My assumption is I need the early close statement so I can correctly loop
through and open then close the various excel files.

I also thought by checking in the Finally block if the wb object is
nothing
would be good in case anything went wrong and a wb instance was left open.
I
guess I don't understand why I'm getting this error when everything does
close properly because I would think the if statement in the finally block
would evaluate to nothing and not execute but instead I get an object
disconnected error.

Any help is greatly appreciated. I'm very new to VB.NET. Thanks

--
-ridawg

--
-ridawg

Sep 26 '06 #2
My thought or concern is that if something happens before the first close
statement is executed and wb is left open then I would catch it in the
Finally block and close it there.

My guess is that doesn't make sense and I should change my code to what you
posted. I'm pretty new to VB.NET and I'm trying to convert a bunch code from
VBA. So I'm learning on the fly. Thanks

--
-ridawg
"Norman Yuan" wrote:
Why do you close the workbook again in "Finally" block? the workbook getting
closed in "Try" block does not mean variable "wb" becomes Nothing, it still
points to the momory where workbook object was hosted there, although the
workbook object has been closed. Variable "wb" will hold that value until it
is out of scope, or you set it to Nothing. Thus, simply because a pointer is
not Nothing, it does not mean you can close an object twice the pointer
pointed to, if the said object has been closed previously. Therefore, you
got the error.

You might only need

Try
...
Catch
....
Finally

If Not appExcel Is Nothing Then

wb = Nothing
appExcel.Quit()
Marshal.FinalReleaseComObject(fr.appExcel)
appExcel = Nothing

End If
End Try
"ridawg" <ri****@discussions.microsoft.comwrote in message
news:46**********************************@microsof t.com...
Hey,

I have some code where I open up Excel then loop through several cases to
update several workbooks.

Basically something like this (not showing all the code just basic
structure).
Try
appExcel = New Excel Application
Do While Counter <=2
Select Case Counter
Case 1
eTemplate = "Some Excel File1"
RName = "New Version of File1"
Case 2
eTemplate = "Some Excel File2"
RName = "New Version of File2"
End select
wb = .Workbooks.Open(RTemplate)
With wb
.RefreshAll()
.SaveAs(RName)
.Close()
End With

'Increment Counter
Counter += 1

Loop
Catch ex As Exception
'Report Error
MessageBox.Show((ex.ToString))
Finally
If Not appExcel Is Nothing Then
GC.Collect()
GC.WaitForPendingFinalizers()

If Not wb Is Nothing Then
wb.Close(SaveChanges:=False)
Marshal.FinalReleaseComObject(fr.wb)
wb = Nothing
End If

appExcel.Quit()
Marshal.FinalReleaseComObject(fr.appExcel)
appExcel = Nothing
End If

End Try

I'm getting an error on the close statement in the Finally block. The
error
states that the object doesn't exist (disconnected). Which makes sense if
everything executes correctly because I have a close statement prior to
this
statement (in the loop code). But I'm not sure why I'm getting the error
because the if statement is checking for nothing.

My assumption is I need the early close statement so I can correctly loop
through and open then close the various excel files.

I also thought by checking in the Finally block if the wb object is
nothing
would be good in case anything went wrong and a wb instance was left open.
I
guess I don't understand why I'm getting this error when everything does
close properly because I would think the if statement in the finally block
would evaluate to nothing and not execute but instead I get an object
disconnected error.

Any help is greatly appreciated. I'm very new to VB.NET. Thanks

--
-ridawg

--
-ridawg


Sep 26 '06 #3

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

Similar topics

0
by: Winshent | last post by:
this code opens the workbook.. either protects or unprotects a sheet.. then saves.. so can write to it.. it unprotects then saves fine.. then writes to it no prob.. it resets the password...
0
by: Bernie Yaeger | last post by:
I have the following code fragment for opening and the closing excel in a vb ..net app: objxl = New Excel.Application objwbs = objxl.Workbooks objwb = objwbs.Add objws =...
2
by: Atley | last post by:
I have written an application that exports data from SQL to Excel. It all works perfectly except that if you open the Task Manager after running my application, there is an instance of Excel in...
0
by: Sania | last post by:
Hi, We have an application that create an excel object on the very beginning of the application: objExcel = New Excel.Application Then I am passing the excel reference to the one of forms...
7
by: rdemyan via AccessMonster.com | last post by:
I want to make sure that I'm closing an opened spreadsheet correctly. I've been having some locking up problems. The following code adds a dummy row to the spreadsheet to ensure that that the data...
0
by: Jono | last post by:
Hello, I've been getting this message when closing excel (not necessarily when closing the workbook by itself, but when closing Excel and the workbook at the same time): ...
1
by: John Bailo | last post by:
I wrote a c# web service that creates an Excel spreadsheet. Even though I follow all the formal procedures for closing the app and finalizing, an instance of Excel still remains in memory. I...
1
by: fakehitswizard | last post by:
this is the correct way to close excel with C#. I've seen alot of other bogus posts ALL over the web that don't work, how frustrating. string savepath; bool foundPID; int ourPID = 0; int...
2
by: Ronin85 | last post by:
Hi , I recently have much pain working with excel application especially closing excel . I try several method but with no success i try to urge garbage collector to dispose excel application object,...
2
by: Silgd1 | last post by:
Hi All.... I'm using python 2.4, Win XP Pro v.2002 sp3, and I use pyscripter 1.9.9.2 as my editor. I have written code to open an existing excel file and grab some data. The problem I am...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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?

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.