+ Reply to Thread
Results 1 to 12 of 12

Excel Session timer 10min splash screen Warning then 15min force close

Hybrid View

  1. #1
    Registered User
    Join Date
    01-19-2009
    Location
    Montreal, Quebec
    MS-Off Ver
    Excel 2003
    Posts
    13

    Excel Session timer 10min splash screen Warning then 15min force close

    Hi, The purpose of this macro was to control the time this excel file was open because many users modify it. I decided to create a splash screen (userform) to display a message to the user saying "your session time of 15min is up"

    The problem is, the first time I open the file everything works fine. But after I close it (just the workbook not excel), It opens itself for some reason and gives me a debug error message. Perhaps the timer is never stopped?


    I call "StartTimer10min" on workbook open and "StopTimer" before workbook close. Thanks for the help

    Here is the code in my module:

    Public RunWhen As Double
    Public Const cRunIntervalSeconds10 = 600 '10min
    Public Const cRunWhat10 = "MsgPrompt"  ' the name of the procedure to run
    
    'This program prompts the user after time has met a preset
    Sub MsgPrompt()
        UserForm2.Show
    End Sub
    
    Sub StartTimer10min()
        RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds10)
        Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat10, _
            Schedule:=True
    End Sub
    
    Sub KillForm()
    Unload UserForm2
    End Sub
    
    Sub StopTimer()
        On Error Resume Next
        Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat10, _
            Schedule:=False
    End Sub
    Last edited by Phil_pac; 02-20-2009 at 01:45 PM. Reason: Title change

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Splash screen after 10min of excel open error

    Post the ThisWorkbook code so we can see the whole thing?
    Last edited by shg; 02-16-2009 at 02:50 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    01-19-2009
    Location
    Montreal, Quebec
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Splash screen after 10min of excel open error

    Workbook Code:

    Private Sub Workbook_Open()
    
    StartTimer10min
    
    End Sub
    
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    StopTimer
    
    End Sub
    UserForm2 code:

    Private Sub UserForm_Initialize()
    Application.OnTime Now + TimeValue("00:00:07"), "KillForm"
    End Sub

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Splash screen after 10min of excel open error

    You also have to unschedule KillForm in the close event.

  5. #5
    Registered User
    Join Date
    01-19-2009
    Location
    Montreal, Quebec
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Splash screen after 10min of excel open error

    I don't know, maybe it's me but My unscheduling was giving me an error so I had to "On Error Resume Next like I did with the other unschedule. (bad programming I know) but it still does not stop it from re-opening.

    This is part of my workbook code

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    StopTimer
    
    On Error Resume Next
      Application.OnTime _
      EarliestTime:=Now + TimeValue("00:00:00"), _
      Procedure:="KillForm", _
      Schedule:=False
    End Sub

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Splash screen after 10min of excel open error

    When you unschedule, you have to pass the time it was scheduled, just like for the other routine.

  7. #7
    Registered User
    Join Date
    01-19-2009
    Location
    Montreal, Quebec
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Excel Session timer 10min splash screen Warning then 15min force close

    Nevermind guysl... For some reason, Everytime I close the file it somehow automatically re-opens later.... This is quite frustrating. Any more Ideas? Maybe my approach is wrong?

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Excel Session timer 10min splash screen Warning then 15min force close

    Where do you unschedule KillForm?

  9. #9
    Registered User
    Join Date
    01-19-2009
    Location
    Montreal, Quebec
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Excel Session timer 10min splash screen Warning then 15min force close

    In the main module, Sub StopTimer()

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Excel Session timer 10min splash screen Warning then 15min force close

    In the Initialize event, you schedule Killform. In the Close event, you're not passing the time it was scheduled. Now + 7 minutes now is not the same as Now + 7 minutes later.

  11. #11
    Registered User
    Join Date
    01-19-2009
    Location
    Montreal, Quebec
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Excel Session timer 10min splash screen Warning then 15min force close

    Ok, so I got it to work without the userform (Since it was giving me problems). instead of a splash screen, I created a msgbox Yes/No event with the same warning. So it goes like this:

    After 10min of opening the file, the user is prompted with the message box saying they've "passed 10min save & close? You have 5 extra minutes" if Yes, then save and close workbook. If no, then a 5min timer starts and at the end of 5min it saves and closes automatically.

    I call only the 10min start timer on workbook open, after message box I stop all timers and then begin the second timer. on workbook close I stop all timers.

    Workbook code:
    Private Sub Workbook_Open()
    StartTimer10min
    end sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    StopTimer
    End Sub
    Here is the module code:

    Public RunWhen As Double
    Public RunWhen2 As Double
    Public Const cRunIntervalSeconds10 = 600    '10min
    Public Const cRunWhat10 = "MsgPrompt"  ' the name of the procedure to run
    Public Const cRunIntervalSeconds15 = 300 '15min
    Public Const cRunWhat15 = "CloseAll"  ' the name of the 2nd procedure to run
    
    'This program prompts the user after time has met a preset
    Sub MsgPrompt()
    Message
    StopTimer
    StartTimer15min
    End Sub
    
    Sub StartTimer10min()
        RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds10)
        Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat10, _
            Schedule:=True
    End Sub
    Sub StartTimer15min()
        RunWhen2 = Now + TimeSerial(0, 0, cRunIntervalSeconds15)
        Application.OnTime EarliestTime:=RunWhen2, Procedure:=cRunWhat15, _
            Schedule:=True
    End Sub
    
    Sub StopTimer()
            On Error Resume Next
        Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat10, _
            Schedule:=False
            
            On Error Resume Next
        Application.OnTime EarliestTime:=RunWhen2, Procedure:=cRunWhat15, _
            Schedule:=False
     End Sub
     
    Sub CloseAll()
    'This closes the file, and saves any changes if changes were made
    Workbooks("Suivi de projets Dpt 6450.xls").Close SaveChanges:=True
    End Sub
    
    Sub Message()
    
    
    Dim Answer As String
    Dim MyNote As String
    
        'Place your text here
        MyNote = "Your 10min session is up, you have 5min before auto save & close. Save&Close Now?"
    
        'Display MessageBox
        Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "SESSION EXPIRED")
    
        If Answer = vbNo Then
        Exit Sub
            'Code for No button Press
        Else
            'Code for Yes button Press
        CloseAll
        End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1