L
lgbjr
Hi All,
I have a VB.NET app that, among other things, writes data to Excel. I am
having trouble getting the Excel process to terminate after I quit Excel. I
found an article related to this problem:
http://support.microsoft.com/default.aspx?scid=kb;en-us;317109
Below is a sample of code that I wrote based on the above article. The excel
workbook, worksheets, and all of the cells are properly formatted when Excel
quits, but the process will not terminate.
I've tried the code as shown in the above article and the process does
terminate, but it seems that as soon as you do something to modify the
workbook, the Excel process won't terminate.
any ideas??
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim c, i, j, r, s, lastcol As Integer
Dim Sname() As String = {"Hello", "World", "Bye"}
Dim T_head As New ArrayList
Dim Lee() As Integer = {0, 1, 2}
Dim Jeanie() As Integer = {3, 4, 5}
Dim we() As Integer = {6, 7, 8}
Dim HIndex() = {Lee, Jeanie, we}
Dim head() As String = {"This", "Is", "A", "Test", "OF", "Working",
"With", "Excel", "Code"}
Dim Rarray As New ArrayList
Dim T_Rarray As New ArrayList
Dim oApp As New Excel.Application
oApp.SheetsInNewWorkbook = 1
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook As Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet
Dim rng As Excel.Range
oApp.Visible = True
oSheet = oBook.ActiveSheet
oSheet.Name = "Lee"
r = 1
For s = 0 To 2
oBook.Sheets.Add()
oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)
For i = 0 To HIndex(s).Length - 1
T_head.Add(head(HIndex(s)(i)))
Next
For c = 0 To T_head.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2 =
T_head(c)
Next
T_head.Clear()
Next
For j = 1 To 3
r = r + 1
For i = 1 To 9
Rarray.Add(i * j)
Next
For s = 0 To 2
For i = 0 To HIndex(s).Length - 1
T_Rarray.Add(Rarray(HIndex(s)(i)))
Next
For c = 0 To T_Rarray.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2 =
T_Rarray(c)
Next
T_Rarray.Clear()
Next
Rarray.Clear()
Next
For s = 0 To 2
oSheet.Select(s + 1)
lastcol = oSheet.UsedRange.Find("*", , , ,
Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious).Column
oSheet.UsedRange.HorizontalAlignment = 3
If (s = 0) Then
oSheet.UsedRange.NumberFormat = 0
End If
oSheet.UsedRange.Columns.AutoFit()
For i = 1 To lastcol
rng = oSheet.Cells(1, i)
rng.Font.Bold = True
rng.Interior.ColorIndex = 15
Next
Next
NAR(rng)
NAR(oSheet)
oBook.Close(False)
NAR(oBook)
NAR(oBooks)
Debug.WriteLine("Sleeping...")
System.Threading.Thread.Sleep(5000)
oApp.Quit()
NAR(oApp)
GC.Collect()
Debug.WriteLine("End Excel")
End Sub
Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub
I have a VB.NET app that, among other things, writes data to Excel. I am
having trouble getting the Excel process to terminate after I quit Excel. I
found an article related to this problem:
http://support.microsoft.com/default.aspx?scid=kb;en-us;317109
Below is a sample of code that I wrote based on the above article. The excel
workbook, worksheets, and all of the cells are properly formatted when Excel
quits, but the process will not terminate.
I've tried the code as shown in the above article and the process does
terminate, but it seems that as soon as you do something to modify the
workbook, the Excel process won't terminate.
any ideas??
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim c, i, j, r, s, lastcol As Integer
Dim Sname() As String = {"Hello", "World", "Bye"}
Dim T_head As New ArrayList
Dim Lee() As Integer = {0, 1, 2}
Dim Jeanie() As Integer = {3, 4, 5}
Dim we() As Integer = {6, 7, 8}
Dim HIndex() = {Lee, Jeanie, we}
Dim head() As String = {"This", "Is", "A", "Test", "OF", "Working",
"With", "Excel", "Code"}
Dim Rarray As New ArrayList
Dim T_Rarray As New ArrayList
Dim oApp As New Excel.Application
oApp.SheetsInNewWorkbook = 1
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook As Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet
Dim rng As Excel.Range
oApp.Visible = True
oSheet = oBook.ActiveSheet
oSheet.Name = "Lee"
r = 1
For s = 0 To 2
oBook.Sheets.Add()
oSheet = oBook.ActiveSheet
oSheet.Name = Sname(s)
For i = 0 To HIndex(s).Length - 1
T_head.Add(head(HIndex(s)(i)))
Next
For c = 0 To T_head.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2 =
T_head(c)
Next
T_head.Clear()
Next
For j = 1 To 3
r = r + 1
For i = 1 To 9
Rarray.Add(i * j)
Next
For s = 0 To 2
For i = 0 To HIndex(s).Length - 1
T_Rarray.Add(Rarray(HIndex(s)(i)))
Next
For c = 0 To T_Rarray.Count - 1
oSheet.Select(s + 1)
CType(oSheet.Cells.Item(r, c + 1), Excel.Range).Value2 =
T_Rarray(c)
Next
T_Rarray.Clear()
Next
Rarray.Clear()
Next
For s = 0 To 2
oSheet.Select(s + 1)
lastcol = oSheet.UsedRange.Find("*", , , ,
Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious).Column
oSheet.UsedRange.HorizontalAlignment = 3
If (s = 0) Then
oSheet.UsedRange.NumberFormat = 0
End If
oSheet.UsedRange.Columns.AutoFit()
For i = 1 To lastcol
rng = oSheet.Cells(1, i)
rng.Font.Bold = True
rng.Interior.ColorIndex = 15
Next
Next
NAR(rng)
NAR(oSheet)
oBook.Close(False)
NAR(oBook)
NAR(oBooks)
Debug.WriteLine("Sleeping...")
System.Threading.Thread.Sleep(5000)
oApp.Quit()
NAR(oApp)
GC.Collect()
Debug.WriteLine("End Excel")
End Sub
Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub