Excel still running after closing it!!!!

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

MoreLikeIt
01-17-2005, 07:31 AM
Hi Martin113,

I have the same problem with closing excel in my .Net application and I didn't find the solution yet, but in the thread "closing excel" there are a few examples you can try.

Is excel still running when you close youre application???

martin113
01-17-2005, 09:25 AM
Thanks for your concern, im going to take a look at that. What i did to solve the problem is declaring the variables in the scope of an event. In my case the click of a button.
That seems to work fine.

Thanks
Martin

Hi Martin113,

I have the same problem with closing excel in my .Net application and I didn't find the solution yet, but in the thread "closing excel" there are a few examples you can try.

Is excel still running when you close youre application???

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum