Results 1 to 14 of 14

Thread: Closing workbooks (oh you'll see)

  1. #1

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Closing workbooks (oh you'll see)

    Alright, here's what I want to do:

    I have a main workbook whose 'close' event is fired when the "X" is pressed to close excel. At the time, the focus is on another workbook (opened via the first one with code)

    I have tried and tried and tried but no matter what I do I either end up with:
    The workbooks close IFF I'm focused on the main workbook
    The main workbook won't close when focused on second workbook
    (tried moving focus to first workbook first, no effect)
    Excel crashes

    Some code:
    VB Code:
    1. On Error Resume Next
    2. Dim Wkb As Workbook
    3. Static Recurse As Boolean
    4.  
    5.     If Recurse Then
    6.         Exit Sub
    7.     End If
    8. 'temp code to stop bug
    9. 'preferably save and close the whole program or just that workbook
    10. 'grey bar shows up if just close that workbook
    11.     If WkbIntro.Name <> ActiveWorkbook.Name Then
    12.         MsgBox "timbrplans can only be closed from the intro page.", vbExclamation, "timbrplans"
    13.         Cancel = True
    14.         Exit Sub
    15.     End If
    16. 'sets updating, events to false, and status bar to "..."
    17.     Safety.SetSettings False, False, True, "Shutting Down..."
    18.    
    19. 'Just closing the program here, no real effect
    20.     CheckSystemData
    21.     Recurse = True
    22.     EnableKeys True
    23.     EnableMenus True
    24.     Application.Caption = "Excel"
    25.    
    26.     'Save and close all of our workbooks
    27.     'by checking if they are open
    28.     For Each Wkb In Workbooks
    29.         Select Case LCase(Wkb.Name)
    30.         Case "tbm_intro.xls"
    31.         Case "tbm_homes.xls", "tbm_swing.xls", "tbm_sheds.xls", _
    32.           "tbm_backyardorganizer.xls", "tbm_decks.xls", _
    33.           "tbm_Bunkie.xls", "tbm_estimator.xls", "tbm_garages.xls"
    34.             SaveMe Wkb
    35.             Wkb.Close False
    36.         Case Else
    37.             Cancel = True
    38.         End Select
    39.     Next Wkb
    40.    
    41.     SaveMe ThisWorkbook
    42. 'default settings for next excel run
    43.     Safety.RestoreSettings
    44.  
    45. 'if an unknown workbook was open, dont kill all of excel
    46.     If Cancel Then
    47.         Application.WindowState = xlNormal
    48.         ThisWorkbook.Close False
    49.     End If
    Don't pay attention to this signature, it's contradictory.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Could you verify that I have it correct.

    You open the main workbook and then open a second one from
    the main one. Then when you close the main one you are trying
    to close the second one but it disallows it.

    It sounds like the order of operations needs defining. All
    secondary workbooks should be closed BEFORE the main
    workbook is closed.

    How close am I
    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
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Way off:

    Check the code, the secondaries are all closed first. I open the main, open the second via the main, then hit "X" in the second.

    The close event in the main is fired (I put a breakpoint in both close events, only the main oen is fired) then runs the sub below.

    It does some cleanup, saves closes the secondary workbook, then saves and closes the main. (are the workbook.close statements getting in the way of the closing?)

    I end up with the main one open and saved.

    I tried putting "thisworkbook.close" at the bottom, but it still leaves excel open, with no workbook (application.quit didn't work, either).
    Don't pay attention to this signature, it's contradictory.

  4. #4

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    By the way this is excel 97
    Don't pay attention to this signature, it's contradictory.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If application.quit doesnt work then you may have a hidden process
    stil open. Check taskmanager for any other instances
    of "EXCEL.EXE". Then try again. I will look closer at your code
    again, but without all the code its hard to debug.
    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

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Excel.exe is obviously still in the task manager if I can see it and open new workbooks.

    The puzzle here is: why does the sub work if and only if it is called when the focus is on the main workbook (where the sub is stored)?
    Don't pay attention to this signature, it's contradictory.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Perhaps I was not clear, but I was refering to multiple instances
    of EXCEL.EXE not a single instance. :grin:

    After looking at your sample code a bit more, I would suggest
    that you use the complete objects instead of shortcuts like
    ThisWorkbook. This will allow you to debug where the issue may
    be coming from.
    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

  8. #8

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    "thisworkbook" refers to the workbook the code is in, the main workbook. I actually changed it from workbooks("tbm_intro.xls") because it looks better.

    ActiveWorkbook is needed because... well sometimes this is called from outside "ThisWorkbook"!

    No, there are not multiple instances of excel.exe

    I also have captions (the "SaveMe" function is a simple three liner that changes the status bar caption + saves) that tell me it's following the correct order of operations.

    Hell, I've gone through it using F8 a few times.
    Don't pay attention to this signature, it's contradictory.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    I know what ThisWorkBook is I was trying to point out that
    "thisworkbook" can change depending on the active workbook,
    not just the one that has the code running. This is why I was
    suggesting complete object references. Its like the way
    AvtiveWorkbook changes between workbooks depending on the
    wb in focus.
    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

  10. #10

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Originally posted by RobDog888
    I know what ThisWorkBook is I was trying to point out that
    "thisworkbook" can change depending on the active workbook,
    not just the one that has the code running. This is why I was
    suggesting complete object references. Its like the way
    AvtiveWorkbook changes between workbooks depending on the
    wb in focus.
    I assumed you knew what it was, I was just saying..

    I had no idea it would change with focus... kindof destroys the point of having thisworkbook and activeworkbook, doesn't it? MS mentality?
    Don't pay attention to this signature, it's contradictory.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Yes, but think of it this way... When the workbook is running
    ThisWorkbook code and you switch workbooks, the
    ThisWorkbook class is changed to reflect the second workbooks
    ThisWorkbook class. ThisWorkbook follows ActiveWorkbook.
    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

  12. #12

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Originally posted by RobDog888
    Yes, but think of it this way... When the workbook is running
    ThisWorkbook code and you switch workbooks, the
    ThisWorkbook class is changed to reflect the second workbooks
    ThisWorkbook class. ThisWorkbook follows ActiveWorkbook.
    So thisworkbook == activeworkbook??

    I always though ThisWorkbook referred to the workbook the code was being run from.
    Don't pay attention to this signature, it's contradictory.

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    No, I mean like the way that ActiveWorkbook changes from workbook
    to workbook. So does ThisWorkbook changes from workbook to
    workbook depending on which one is active.
    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

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If you have two workbooks open and each one has code in ThisWorkbook,
    the only way Excel can tell which one it refers to is to check which
    one is active.

    Same thing with ActiveWorkbook, Excel know which workbook
    ActiveWorkbook is by which one is currently active.
    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