|
-
Jun 4th, 2008, 01:27 AM
#6
Re: BackgroundWorker - Destroy Excel Instance
Ok found the issue. I had missed it. In addition to changing the declaration of the vars, there was a scope issue with the Excel object varin your loop. Since its not destroyed in the loop it will also cause the retension of the instance of Excel.
I wrote a variation of your code for testing and Excel now terminates properly without the GC calls.
Code:
Option Explicit On
Option Strict On
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim oExcel As Excel.Application = DirectCast(CreateObject("Excel.Application"), Excel.Application)
oExcel.Visible = False
Dim oWBs As Excel.Workbooks = oExcel.Workbooks
Dim oWB As Excel.Workbook = oWBs.Open(CType("C:\Book1.xls", String))
Dim WSNames(oWB.Worksheets.Count - 1) As String
For intIdx As Integer = 0 To oWB.Worksheets.Count - 1
Dim oWS As Excel.Worksheet = DirectCast(oWB.Worksheets.Item(intIdx + 1), Excel.Worksheet)
' Load up the Array
WSNames(intIdx) = oWS.Name
' Calculate the % complete and pass out to update the Progressbar
'worker.ReportProgress((intIdx + 1) * CInt(100 / (oWBs.Count)))
MessageBox.Show(WSNames(intIdx).ToString)
oWS = Nothing
Next
If oExcel IsNot Nothing Then
oWBs = Nothing
oWB.Close(SaveChanges:=False)
oWB = Nothing
oExcel.Quit()
oExcel = Nothing
End If
' Pass out the Array of Worksheet Names to the completed BackgroundWorker
'e.Result = WSNames
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
End Class
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|