Z
zino
the following procedure in visual studio 2005, works fine and the Excel
process can be released successfully, but when I try to use the Style
propertie (between doted lines below), it looks that some hidden object is
created which I'm not able to release:
Public sub SaveExcel(ByVal filePath As String)
Dim xl As Excel.Application
Dim wbs As Excel.Workbooks
Dim wb As Excel.Workbook
Dim sheets As Excel.Sheets
xl = New Excel.Application
wbs = xl.Workbooks
wb = wbs.Add()
sheets = wb.Worksheets
xl.Visible = False
Dim range As Excel.Range = xl.ActiveCell
sheets(1).Range("A1") = "Client code:"
'---------------------------------------------------------------------------
Dim labelStyle As Excel.Style
labelStyle = wb.Styles.Add("myStyle")
..... ...
... .
' ----------------------------------------------------------------------------
ReleaseComObject(labelStyle)
labelStyle = Nothing
xl.DisplayAlerts = False
wb.SaveAs(filePath, FileFormat:=Excel.XlFileFormat.xlExcel7)
ReleaseComObject(range)
Dim sheet As Excel.Worksheet
For Each sheet In sheets
ReleaseComObject(sheet)
Next
ReleaseComObject(sheets)
wb.Close(False)
xl.Quit()
ReleaseComObject(wb)
ReleaseComObject(wbs)
ReleaseComObject(xl)
End sub
Private Sub ReleaseComObject(ByRef Reference As Object)
Try
Do Until System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference)
<= 0
Loop
Catch
Finally
Reference = Nothing
End Try
End Sub
thanks for help.
process can be released successfully, but when I try to use the Style
propertie (between doted lines below), it looks that some hidden object is
created which I'm not able to release:
Public sub SaveExcel(ByVal filePath As String)
Dim xl As Excel.Application
Dim wbs As Excel.Workbooks
Dim wb As Excel.Workbook
Dim sheets As Excel.Sheets
xl = New Excel.Application
wbs = xl.Workbooks
wb = wbs.Add()
sheets = wb.Worksheets
xl.Visible = False
Dim range As Excel.Range = xl.ActiveCell
sheets(1).Range("A1") = "Client code:"
'---------------------------------------------------------------------------
Dim labelStyle As Excel.Style
labelStyle = wb.Styles.Add("myStyle")
..... ...
... .
' ----------------------------------------------------------------------------
ReleaseComObject(labelStyle)
labelStyle = Nothing
xl.DisplayAlerts = False
wb.SaveAs(filePath, FileFormat:=Excel.XlFileFormat.xlExcel7)
ReleaseComObject(range)
Dim sheet As Excel.Worksheet
For Each sheet In sheets
ReleaseComObject(sheet)
Next
ReleaseComObject(sheets)
wb.Close(False)
xl.Quit()
ReleaseComObject(wb)
ReleaseComObject(wbs)
ReleaseComObject(xl)
End sub
Private Sub ReleaseComObject(ByRef Reference As Object)
Try
Do Until System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference)
<= 0
Loop
Catch
Finally
Reference = Nothing
End Try
End Sub
thanks for help.