Results 1 to 5 of 5

Thread: [RESOLVED] Can not kill Excel Objects....VB

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Bangalore
    Posts
    172

    Resolved [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

  2. #2
    Lively Member djklocek's Avatar
    Join Date
    Aug 2006
    Posts
    107

    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:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    2. 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
    3. Const WM_CLOSE = &H10
    4. Private Sub Form_Load()
    5. Dim winHwnd As Long
    6. Dim RetVal As Long
    7. winHwnd = FindWindow(vbNullString, "Calculator")
    8. If winHwnd <> 0 Then
    9. PostMessage winHwnd, WM_CLOSE, 0&, 0&
    10. Else
    11. MsgBox "The Calculator is not open."
    12. End If
    13. End Sub

    Thanx to AllAPI

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Bangalore
    Posts
    172

    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

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width