Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

program logic question 1

Status
Not open for further replies.

nacco

Programmer
Nov 5, 2003
8
NL
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
 
Don't declare it as a new object globally. Declare it as an excel object but create a new instance of it from within the sub.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks ca8msm.

I deleted the New and added
xlApp = New Excel.Application
in my sub.

again thanks for your help.

wim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top