mskeel
Well-known member
Im following these pages from Mircrosoft:
http://support.microsoft.com/?kbid=317109
http://support.microsoft.com/kb/306022/
I have created the following methods that are called when you click on a button.
This is my code that Ive created to reproduce the results and more closely match the automated excel output of my data:
As you can see, the only thing that I changed was what was written in the "body" of the method -- I followed the same basic formula for setting up my Excel objects, doing something, and then shutting everything down.
Heres the part that doesnt make any sense. If you run these methods, the first one will execute and then kill the Excel process like it is supposed to do. The second method will not. Both methods succesfully write what they are supposed to, but the second one does not properly kill Excel when it is finished. Whats going on here?
Now heres something that is even more annoying. Make a third method in a new button handler method that looks like this:
Again, the same basic formula but this one works as it is supposed to. The major differences -- the Range is not calculated on the fly with an i.ToString() and the number of i.ToString()s assigned to Values are reduced. Recompile and rerun the program, this time uncommenting the ToString I have commented out above ("Uncomment me for something that doesnt make sense!"). Surprisingly, Excel doesnt clean up any more.
What is going on here? Am I missing some great fundamental truth of talking to Excel via COM? Why does this work sometimes but not others?
http://support.microsoft.com/?kbid=317109
http://support.microsoft.com/kb/306022/
I have created the following methods that are called when you click on a button.
Code:
Almost a verbatim copy of the sample code in one of the links above
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
Add data to cells of the first worksheet in the new workbook.
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Last Name"
oSheet.Range("B1").Value = "First Name"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = "Doe"
oSheet.Range("B2").Value = "John"
oSheet.Range("A3").Value = "BARF"
oSheet.Range("B3").Value = "POISON"
Save the Workbook and quit Excel.
oBook.SaveAs("C:\Documents and Settings\test\Desktop\" & "Book1.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
End Sub
This is my code that Ive created to reproduce the results and more closely match the automated excel output of my data:
Code:
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
Set the headers, colunm-wise
oSheet = oBook.Worksheets(1)
oSheet.Range("B1").Value = "Some"
oSheet.Range("C1").Value = "Header"
oSheet.Range("D1").Value = "For me"
oSheet.Range("E1").Value = "TOOOOOOOoooo"
oSheet.Range("F1").Value = "Experiment"
oSheet.Range("G1").Value = "WITH!"
oSheet.Range("B1", "G1").ColumnWidth = 20
oSheet.Range("B1", "G1").Font.Bold = True
currentWorksheet.Range("B2", "G2").HorizontalAlignment = xlCenter
oSheet.Range("A1").ColumnWidth = 50
For i As Integer = 0 To 5
oSheet.Range("A" + (i + 2).ToString()).Value = "Super" + i.ToString + " - " + "Duper" + (i * 5).ToString
oSheet.Range("B" + (i + 2).ToString()).Value = i.ToString + i.ToString
oSheet.Range("C" + (i + 2).ToString()).Value = "MEANNNN" + i.ToString
oSheet.Range("D" + (i + 2).ToString()).Value = "MEGAAAAA!!" + i.ToString
oSheet.Range("E" + (i + 2).ToString()).Value = "ULTRA MEGA" + i.ToString
oSheet.Range("F" + (i + 2).ToString()).Value = "WICKED AWESOME" + i.ToString
oSheet.Range("G" + (i + 2).ToString()).Value = "GREATOO" + i.ToString
Next
Save the Workbook and quit Excel.
oBook.SaveAs("C:\Documents and Settings\test\Desktop\" & "Super.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
End Sub
As you can see, the only thing that I changed was what was written in the "body" of the method -- I followed the same basic formula for setting up my Excel objects, doing something, and then shutting everything down.
Heres the part that doesnt make any sense. If you run these methods, the first one will execute and then kill the Excel process like it is supposed to do. The second method will not. Both methods succesfully write what they are supposed to, but the second one does not properly kill Excel when it is finished. Whats going on here?
Now heres something that is even more annoying. Make a third method in a new button handler method that looks like this:
Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
Set the headers, colunm-wise
oSheet = oBook.Worksheets(1)
oSheet.Range("B1").Value = "Some"
oSheet.Range("C1").Value = "Header"
oSheet.Range("D1").Value = "For me"
oSheet.Range("E1").Value = "TOOOOOOOoooo"
oSheet.Range("F1").Value = "Experiment"
oSheet.Range("G1").Value = "WITH!"
oSheet.Range("B1", "G1").ColumnWidth = 20
oSheet.Range("B1", "G1").Font.Bold = True
currentWorksheet.Range("B2", "G2").HorizontalAlignment = xlCenter
oSheet.Range("A1").ColumnWidth = 50
For i As Integer = 0 To 5
oSheet.Range("A2").Value = "AYEEE!"
oSheet.Range("B2").Value = "Bogus" + i.ToString Uncomment me for something that doesnt make sense!
oSheet.Range("C2").Value = "MEANNNN" + i.ToString
oSheet.Range("D2").Value = "MEGAAAAA!!" + i.ToString
oSheet.Range("E2").Value = "ULTRA MEGA" + i.ToString
oSheet.Range("F2").Value = "WICKED AWESOME" + i.ToString
oSheet.Range("G2").Value = "GREATOO" + i.ToString
Next
Save the Workbook and quit Excel.
oBook.SaveAs(Me._fileName)
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
End Sub
Again, the same basic formula but this one works as it is supposed to. The major differences -- the Range is not calculated on the fly with an i.ToString() and the number of i.ToString()s assigned to Values are reduced. Recompile and rerun the program, this time uncommenting the ToString I have commented out above ("Uncomment me for something that doesnt make sense!"). Surprisingly, Excel doesnt clean up any more.
What is going on here? Am I missing some great fundamental truth of talking to Excel via COM? Why does this work sometimes but not others?