Hi group,
I'm discovering that my Excel spreadsheets that are being created by a VB.net app I've written isn't completely closing them as they should be. I'm struggling to understand why and how to fix this. Here is the code for the portion of the app that creates the workbook, inserts the data and closes the workbook (or it should be). Do you see some commands that I may be missing (they are at the bottom of the code)?
xlApp = New Excel.Application
xlApp.Visible = False
' Add a new workbook.
xlWorkBook = xlApp.Workbooks.Add
xlWorkSheet = CType(xlWorkBook.ActiveSheet, Worksheet)
If System.IO.File.Exists(DASRPTFileName) = True Then
System.IO.File.Delete(DASRPTFileName)
End If
rowXl = 1
Dim DASDay As String = ""
Dim DASMonth As String = ""
Dim DASYear As String = ""
Dim DASDateTest As String = ""
Dim DASDateSerial As Double = 0
Dim DASDate As Date
Dim propID As String
Dim headers() As String = {"Prop No/Date", "Non-Guaranteed", "Guaranteed", "Stayovers", "Departures", "Adult", "Youth", "Children", "Sold", "OOO", "Off", "Not Sold", "Occ %", "Proj Revenue", "Avg Rate"}
For col As Integer = 0 To UBound(headers)
xlWorkSheet.Cells(1, col + 1) = headers(col)
Next
xlRange = xlWorkSheet.Range("A1", "O1")
xlRange.WrapText = True
xlRange.Interior.Color = Color.LightGray
xlRange.HorizontalAlignment = -4108
rowXl = rowXl + 1
Dim objReader3 As New System.IO.StreamReader(DASRPT14Day)
Do While objReader3.Peek() <> -1
txtLine = objReader3.ReadLine()
' first convert date into Month, Day and Year (ex June 1, 2016)
propID = Trim(Microsoft.VisualBasic.Left(txtLine, 5))
DASDay = Microsoft.VisualBasic.Mid(txtLine, 7, 2)
DASMonth = Microsoft.VisualBasic.Mid(txtLine, 10, 3)
DASYear = Microsoft.VisualBasic.Mid(txtLine, 14, 2)
DASDateTest = DASMonth & " " & DASDay & ", 20" & DASYear
If IsNumeric(DASDay) = True Then
' converting the above date into a date number that Excel can read
DASDate = CDate(DASDateTest)
DASDateSerial = (Date.Parse(DASDateTest).ToOADate)
If DASDateSerial < advDateSerial Then
' combining the property number with the Excel date to create the field "propAndDate"
propAndDate = propID & "-" & DASDateSerial
nonGTD = CInt(Microsoft.VisualBasic.Mid(txtLine, 21, 8))
GTD = CInt(Microsoft.VisualBasic.Mid(txtLine, 30, 5))
stayovers = CInt(Microsoft.VisualBasic.Mid(txtLine, 36, 5))
departures = CInt(Microsoft.VisualBasic.Mid(txtLine, 42, 6))
adults = CInt(Microsoft.VisualBasic.Mid(txtLine, 49, 4))
youth = CInt(Microsoft.VisualBasic.Mid(txtLine, 54, 4))
children = CInt(Microsoft.VisualBasic.Mid(txtLine, 59, 4))
sold = CInt(Microsoft.VisualBasic.Mid(txtLine, 64, 5))
ooo = CInt(Microsoft.VisualBasic.Mid(txtLine, 70, 5))
off = CInt(Microsoft.VisualBasic.Mid(txtLine, 76, 5))
notSold = CInt(Microsoft.VisualBasic.Mid(txtLine, 82, 5))
occPcnt = CDbl(Microsoft.VisualBasic.Mid(txtLine, 88, 10))
revenue = CDbl(Microsoft.VisualBasic.Mid(txtLine, 99, 14))
avgRate = CDbl(Microsoft.VisualBasic.Mid(txtLine, 114, 13))
xlWorkSheet.Cells(rowXl, 1) = propAndDate
xlWorkSheet.Cells(rowXl, 2) = nonGTD
xlWorkSheet.Cells(rowXl, 3) = GTD
xlWorkSheet.Cells(rowXl, 4) = stayovers
xlWorkSheet.Cells(rowXl, 5) = departures
xlWorkSheet.Cells(rowXl, 6) = adults
xlWorkSheet.Cells(rowXl, 7) = youth
xlWorkSheet.Cells(rowXl, 8) = children
xlWorkSheet.Cells(rowXl, 9) = sold
xlWorkSheet.Cells(rowXl, 10) = ooo
xlWorkSheet.Cells(rowXl, 11) = off
xlWorkSheet.Cells(rowXl, 12) = notSold
xlWorkSheet.Cells(rowXl, 13) = occPcnt
xlWorkSheet.Cells(rowXl, 14) = revenue
xlWorkSheet.Cells(rowXl, 15) = avgRate
rowXl = rowXl + 1
End If
End If
Loop
xlRange = xlWorkSheet.Range("A:C")
xlRange.ColumnWidth = 11.71
xlRange = xlWorkSheet.Range("D:E")
xlRange.ColumnWidth = 10.43
xlRange = xlWorkSheet.Range("F:I")
xlRange.ColumnWidth = 8.71
xlRange = xlWorkSheet.Range("J:L")
xlRange.ColumnWidth = 6.86
xlRange = xlWorkSheet.Range("M:O")
xlRange.ColumnWidth = 10.29
xlRange.NumberFormat = "#,##0.00"
xlApp.DisplayAlerts = False
xlWorkBook.SaveAs(DASRPTFileName, FileFormat:=51, CreateBackup:=False)
xlWorkBook.Close()
xlApp.DisplayAlerts = True
xlRange = Nothing
xlWorkSheet = Nothing
xlWorkBook = Nothing
xlApp.Quit()
xlApp = Nothing
objReader3.Close()
If System.IO.File.Exists(DASRPT14Day) = True Then
System.IO.File.Delete(DASRPT14Day)
End If
I'm hoping you see something I've overlooked. In advance, thanks for your help.
Don