hey all,
I'm coming from VB6 to VB.NET and want to create a webpage.
I have a program logic question about objects creating and closing:
I need to create an Excel file and define the xl application at class level with public shared.
when the user clicks the generate button I create the excel file and close everything(books, sheets and application).
but suppose he clicks the generate button again, the xl application object is gone. I can't define the xl at a lower level (in sub) because there are more subs who use the object.
How do i solve this ?
public Class WebForm1
Inherits System.Web.UI.Page
Public Shared xlApp As New Excel.Application
Public Shared xlBooks As Excel.Workbooks
Public Shared xlBook As Excel.Workbook
Public Shared xlSheet As Excel.Worksheet
...
Private Sub buttonGenerateFile_Click(ByVal.....
xlBooks = xlApp.Workbooks
xlBook = xlBooks.Add
xlBook.SaveAs(ExcelFileName)
' xlSheets = xlBook.Worksheets.Add
xlSheet = xlBook.Worksheets(1)
For Each chkItem In CheckBoxListTruck.Items
If chkItem.Selected = True Then
generateExcel() ' fill sheet
End If
Next
xlBook.Save()
Try
xlSheet = Nothing
xlBook.Close()
xlBook = Nothing
xlBooks.Close()
xlBooks = Nothing
xlApp.Quit()
xlApp = Nothing
Catch
End Try
Finally
System.GC.Collect()
System.GC.WaitForPendingFinalizers()
End Try
End Sub
Thanks in advance,
wim
I'm coming from VB6 to VB.NET and want to create a webpage.
I have a program logic question about objects creating and closing:
I need to create an Excel file and define the xl application at class level with public shared.
when the user clicks the generate button I create the excel file and close everything(books, sheets and application).
but suppose he clicks the generate button again, the xl application object is gone. I can't define the xl at a lower level (in sub) because there are more subs who use the object.
How do i solve this ?
public Class WebForm1
Inherits System.Web.UI.Page
Public Shared xlApp As New Excel.Application
Public Shared xlBooks As Excel.Workbooks
Public Shared xlBook As Excel.Workbook
Public Shared xlSheet As Excel.Worksheet
...
Private Sub buttonGenerateFile_Click(ByVal.....
xlBooks = xlApp.Workbooks
xlBook = xlBooks.Add
xlBook.SaveAs(ExcelFileName)
' xlSheets = xlBook.Worksheets.Add
xlSheet = xlBook.Worksheets(1)
For Each chkItem In CheckBoxListTruck.Items
If chkItem.Selected = True Then
generateExcel() ' fill sheet
End If
Next
xlBook.Save()
Try
xlSheet = Nothing
xlBook.Close()
xlBook = Nothing
xlBooks.Close()
xlBooks = Nothing
xlApp.Quit()
xlApp = Nothing
Catch
End Try
Finally
System.GC.Collect()
System.GC.WaitForPendingFinalizers()
End Try
End Sub
Thanks in advance,
wim