473,472 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3389
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: 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
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...
1
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...
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.