Results 1 to 10 of 10

Thread: [RESOLVED] Excel process not closing

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Resolved [RESOLVED] Excel process not closing

    I have a simple procedure that opens an Excel workbook (Excel 10) and updates it. However even after Closing, Quiting and setting all objects to nothing the Excel process does not shut down - until the VB application is closed.

    I was able to replicate the problem with this code which does nothing more than open the book and reference some objects.

    VB Code:
    1. Dim objExcel As Excel.Application
    2.     Dim objBook As Excel.Workbook
    3.     Dim objSheet As Excel.Worksheet
    4.     Dim objRange As Range
    5.    
    6.     Set objExcel = New Excel.Application
    7.     Set objBook = objExcel.Workbooks.Open("C:\Testing.xls")
    8.                
    9.     Set objSheet = objBook.Worksheets(1)                
    10.     Set objRange = objSheet.Range(Cells(1, 1), Cells(1, 2))
    11.  
    12.     Set objRange = Nothing
    13.     Set objSheet = Nothing
    14.    
    15.     objBook.Close False
    16.     Set objBook = Nothing
    17.    
    18.     objExcel.Quit
    19.     Set objExcel = Nothing

    The Excel process will shutdown if I comment out this line
    Set objRange = objSheet.Range(Cells(1, 1), Cells(1, 2))

    Something about that Range object is keeping Excel alive. Is there another method I should be using that will release the Range object? Setting it to nothing does not work.
    Last edited by brucevde; Jul 13th, 2006 at 04:27 PM.

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

    Re: Excel process not closing

    Change your line where you dimension your range object.

    VB Code:
    1. 'Form:
    2. Dim objRange As Range
    3.  
    4. 'To:
    5. Dim objRange As Exce.Range
    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

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Excel process not closing

    How would that make a difference?

    I did try it though but Excel still won't shut down.

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

    Re: Excel process not closing

    Because when you reference just the default object without fully qualifying it with Excel. your creating a secondary reference to Excel. If you close one without explicitly closing the other then it will cause it to hang. So unless your coding in Excel VBA you should never use the default implied objects.

    Did you make sure you started out with no hidden Excel.exe processes running?

    Is there any macros in your testing.xls file?
    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

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

    Re: Excel process not closing

    I hear more about processes hanging with 2002 then anything else so far. The test code works fine for me under 2003. Have you done all the Office Updates and/or a Detect and Repair from the Help menu?
    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

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

    Re: Excel process not closing

    I just seen the reference to the default Cells objects. Those should be changed too.
    VB Code:
    1. Set objRange = objSheet.Range(Cells(1, 1), Cells(1, 2))
    2.  
    3. 'to
    4. Set objRange = objSheet.Range(objSheet.Cells(1, 1), objSheet.Cells(1, 2)) 'Or such
    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

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Excel process not closing

    Thanks anyway Rob.

    I was able to get the Excel process to shutdown if I used this statement instead.

    VB Code:
    1. Set objRange = objSheet.Range(objSheet.Cells(1, 1), objSheet.Cells(1, 2))
    2.  
    3. 'or this
    4.  
    5. With objSheet
    6.    Set objRange = .Range(.Cells(1, 1), .Cells(1, 2))
    7. End With

    Not sure why it makes a difference using the Global.Cells method vs the Sheet.Cells method but problem solved.

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

    Re: Excel process not closing

    I beat your post by a minute.

    Like I said, its the implied reference that basically, for lack of a better explaination, duplicates a secondary Excel instance so to speak. Then by not destroying it it holds the object in memory causing Excel to remain even if your other fully referenced object vars are closed and destroyed.
    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

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Excel process not closing

    I beat your post by a minute.
    Umm.. the phone rang...

    I guess these "secondary Excel objects" would be inaccessible from the VB program

    I tried this line as well but it did not work

    Set objRange = objSheet.Range(objExcel.Cells(1, 1), objExcel.Cells(1, 2))

    So bottom line never use any methods listed in the Object Browser under Globals.

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

    Re: Excel process not closing

    Yes. But you need to access the .Cells from the sheet object and not the application opbject.

    'No
    objExcel.Cells(1, 1)

    'Yes
    objSheet.Cells(1, 1)

    You need to multitask like I do then
    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