martin113
01-15-2005, 09:19 PM
Can u tell me what is wrong??
It looks like my Closing of excel doesn't work, after i Close it, i can still find it in the Task Manager
Thanks
MArtin
Here is the code :
Public Class frmNewCase
Inherits System.Windows.Forms.FormPublic Class frmNewCase
Inherits System.Windows.Forms.Form
' Excel Variables ---------------------------------------
Dim xlApp As New Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim xlRng As Excel.Range
Private Sub btnAssign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssign.Click
If txtname.Text = "" Then
MsgBox("Please enter the name of the case", MsgBoxStyle.Information)
txtname.Select()
Else
xlWB = xlApp.Workbooks.Open(excelpath & xls, , False)
xlWS = DirectCast(xlWB.Worksheets("Sheet1"), Excel.Worksheet)
xlRng = WS_LastCellInColumn(xlWS, 1)
intLastRow = xlRng.Row + 1
'Get the Max value as the data is not in order
'Using the function provided by herilane
xlRng = DirectCast(xlWS.Columns(1), Excel.Range)
intMaxValue = xlApp.WorksheetFunction.Max(xlRng)
'Calculate the new value
intMaxValue = intMaxValue + 5
txtcaseno.Text = intMaxValue
Excelyes = True
txtcaseno.Enabled = False
txtname.Enabled = False
End If
End Sub
Shared Function WS_LastCellInColumn(ByVal xlWS As Excel.Worksheet, _
ByVal colNum As Integer) As Excel.Range
Dim numRows As Integer = xlWS.Rows.Count
Dim lastCell As Excel.Range
lastCell = DirectCast(xlWS.Cells(numRows, colNum), Excel.Range)
lastCell = lastCell.End(Excel.XlDirection.xlUp)
WS_LastCellInColumn = lastCell
End Function
'in the Save button i close excel :
If Excelyes Then
'Fill the new Row with data
xlRng = xlWS.Range("A" & intLastRow)
xlRng = DirectCast(xlWS.Cells(intLastRow, 1), Excel.Range)
xlRng.Value = intMaxValue
xlRng = DirectCast(xlWS.Cells(intLastRow, 2), Excel.Range)
xlRng.Value = txtname.Text
'Cleanup
xlRng = Nothing
xlWB.Save()
xlWB.Close(SaveChanges:=False)
xlApp.Quit()
xlApp = Nothing
xlWB = Nothing
xlWS = Nothing
GC.Collect()
End If
Me.Close()
End If
It looks like my Closing of excel doesn't work, after i Close it, i can still find it in the Task Manager
Thanks
MArtin
Here is the code :
Public Class frmNewCase
Inherits System.Windows.Forms.FormPublic Class frmNewCase
Inherits System.Windows.Forms.Form
' Excel Variables ---------------------------------------
Dim xlApp As New Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim xlRng As Excel.Range
Private Sub btnAssign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssign.Click
If txtname.Text = "" Then
MsgBox("Please enter the name of the case", MsgBoxStyle.Information)
txtname.Select()
Else
xlWB = xlApp.Workbooks.Open(excelpath & xls, , False)
xlWS = DirectCast(xlWB.Worksheets("Sheet1"), Excel.Worksheet)
xlRng = WS_LastCellInColumn(xlWS, 1)
intLastRow = xlRng.Row + 1
'Get the Max value as the data is not in order
'Using the function provided by herilane
xlRng = DirectCast(xlWS.Columns(1), Excel.Range)
intMaxValue = xlApp.WorksheetFunction.Max(xlRng)
'Calculate the new value
intMaxValue = intMaxValue + 5
txtcaseno.Text = intMaxValue
Excelyes = True
txtcaseno.Enabled = False
txtname.Enabled = False
End If
End Sub
Shared Function WS_LastCellInColumn(ByVal xlWS As Excel.Worksheet, _
ByVal colNum As Integer) As Excel.Range
Dim numRows As Integer = xlWS.Rows.Count
Dim lastCell As Excel.Range
lastCell = DirectCast(xlWS.Cells(numRows, colNum), Excel.Range)
lastCell = lastCell.End(Excel.XlDirection.xlUp)
WS_LastCellInColumn = lastCell
End Function
'in the Save button i close excel :
If Excelyes Then
'Fill the new Row with data
xlRng = xlWS.Range("A" & intLastRow)
xlRng = DirectCast(xlWS.Cells(intLastRow, 1), Excel.Range)
xlRng.Value = intMaxValue
xlRng = DirectCast(xlWS.Cells(intLastRow, 2), Excel.Range)
xlRng.Value = txtname.Text
'Cleanup
xlRng = Nothing
xlWB.Save()
xlWB.Close(SaveChanges:=False)
xlApp.Quit()
xlApp = Nothing
xlWB = Nothing
xlWS = Nothing
GC.Collect()
End If
Me.Close()
End If