Consulting

Results 1 to 9 of 9

Thread: Time Limit?

  1. #1
    VBAX Regular
    Joined
    Dec 2004
    Posts
    26
    Location

    Time Limit?

    Is there a way of setting a time limit on how long an Excel application can be open before it shuts itself down?

    Frequently, one of our network users leaves the spreadsheet open and then wanders off, forcing others to keep going to his computer to close the file.

    Thanks for your help.

  2. #2
    VBAX Contributor Richie(UK)'s Avatar
    Joined
    May 2004
    Location
    UK
    Posts
    188
    Location
    Hi,

    One approach would be to use OnTime. Try this:

    In the ThisWorkbook object module:

    Private Sub Workbook_Open()
        MsgBox "This workbook will auto-close after 5 minutes of inactivity"
        Call SetTime
    End Sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Call Disable
    End Sub
    
    Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
        Call Disable
        Call SetTime
    End Sub
    
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
        Call Disable
        Call SetTime
    End Sub
    And in a general module:

    Dim DownTime As Date
    
    Sub SetTime()
        DownTime = Now + TimeValue("00:05:00")
        Application.OnTime DownTime, "ShutDown"
    End Sub
    
    Sub ShutDown()
        ThisWorkbook.Save
        ThisWorkbook.Close
    End Sub
    
    Sub Disable()
        On Error Resume Next
        Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", Schedule:=False
    End Sub
    HTH

  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Would sharing the workbook be an option? That way multiple users could have it open at the same time.

    Tools | Share Workbook...

  4. #4
    VBAX Mentor Brandtrock's Avatar
    Joined
    Jun 2004
    Location
    Titonka, IA
    Posts
    399
    Location
    Quote Originally Posted by DRJ
    Would sharing the workbook be an option? That way multiple users could have it open at the same time.

    Tools | Share Workbook...
    Outrider - be aware that this limits some of Excel's features. Don't know if this will matter in your specific case, but thought I'd mention it.

    HTH,
    Brandtrock




  5. #5
    VBAX Contributor Aaron Blood's Avatar
    Joined
    Sep 2004
    Location
    Palm Beach, Florida, USA
    Posts
    130
    Location
    I got a couple examples you might be interested in...

    This one closes after a set time:
    http://www.xl-logic.com/xl_files/vba/shutdown_1.zip

    This one closes after a set time of user inactivity:
    http://www.xl-logic.com/xl_files/vba/shutdown_2.zip
    (might be better for your situation.)

  6. #6
    VBAX Regular
    Joined
    Dec 2004
    Posts
    26
    Location

    Time Limit

    Thanks for all the help.

    DRJ, sharing the spreadsheet would be a disaster with some of our operators, some of them are excellent at destroying data and claiming "It was like that when I opened it"

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Outrider,

    It looks like you've got the answers you're looking for, so I'm going to mark this one solved. Not sure if you are aware that you can actually do that yourself here! It's in your Thread Tools menu at the top of your posts.

    If it's not solved, by all means post back though!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  8. #8
    VBAX Regular
    Joined
    Dec 2004
    Posts
    26
    Location

    Almost Solved

    The code from Aaron Blood (Shutdown 2) is just what I need, but how do I get it to run as soon as my spreadsheet is opened?.

    Thanks.

  9. #9
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Outrider,

    I've "unmarked" your thread solved now, so that we can get you all fixed up. Sorry about that!

    In Aaron's example, there are some required routines in the ThisWorbook module. The relevant pieces to fire it are:

    In the ThisWorkbook module
    [vba]Private Sub Workbook_Open()
    Call SetTime
    End Sub[/vba]

    And in a Standard module
    [vba]Dim DownTime As Date

    Sub SetTime()
    DownTime = Now + TimeValue("00:00:30")
    Application.ontime DownTime, "ShutDown"
    End Sub

    Sub ShutDown()
    ActiveWorkbook.Saved = True
    ActiveWorkbook.Close
    End Sub[/vba]

    What happens is when the workbook opens, the Workbook_Open event fires, calling the SetTime routine. That routine sets the workbook to shut down in 30 seconds.

    FYI, the rest of the code stops it from doing it if the user is actively doing something, so it is all required as well.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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