[RESOLVED] Can not kill Excel Objects....VB
Hi i am handing Excel file with vb. but even i close the application Excel.exe will be their in Windows Task Manager. so whenevr i run my code, Excel.exe count increses....so at one stage i can not open excel file. since its objects are not closed.
In clode i have clearly closed anf kille dthe objects
like,
myExcel.Close
set myExcel as nothing
set myWorkSheet as nothing
set myWorkBook as nothing
But in task manager i can see Excel.exe
Please reply
Re: Can not kill Excel Objects....VB
You can always use the FindWindow-function and the PostMessage-function to find the wanted application, and then send it a message that it has to close itself. One disadvantage: you'll need the exact caption of this application.
Try this
VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Private Sub Form_Load()
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "Calculator")
If winHwnd <> 0 Then
PostMessage winHwnd, WM_CLOSE, 0&, 0&
Else
MsgBox "The Calculator is not open."
End If
End Sub
Thanx to AllAPI
Re: Can not kill Excel Objects....VB
Its bad programming practice to try to terminate a running process or sending the close message to it. Its better to find the offending code and correct it. You should never need to use a workaround to close Excel when you are automating it if its done correctly. :)
Its all about disposing of the Excel objects in the proper order in order to prevent the orphaning of the object heirarchy. Also, never use the implicit Excel objects without using an object variable for them. Ex. Range object.
Any sheet or range object variables set to Nothing.
Then .Close any Workbooks.
Then set your workbook(s) = Nothing
Then .Quit your Excel.Application object variable.
Then set it equal to Nothing
Re: Can not kill Excel Objects....VB
Thank you very much
actually i had one more reference which is not closed...
I made it nothing....
Thanks alot for reply
Quote:
Originally Posted by RobDog888
Its bad programming practice to try to terminate a running process or sending the close message to it. Its better to find the offending code and correct it. You should never need to use a workaround to close Excel when you are automating it if its done correctly. :)
Its all about disposing of the Excel objects in the proper order in order to prevent the orphaning of the object heirarchy. Also, never use the implicit Excel objects without using an object variable for them. Ex. Range object.
Any sheet or range object variables set to Nothing.
Then .Close any Workbooks.
Then set your workbook(s) = Nothing
Then .Quit your Excel.Application object variable.
Then set it equal to Nothing
Re: [RESOLVED] Can not kill Excel Objects....VB
It was the order and not closing of the workbook just from what I seen in your first post.
Glad to have helped. :)