G
Guest
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
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