+ Reply to Thread
Results 1 to 12 of 12
  1. #1
    Registered User
    Join Date
    09-19-2009
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2003
    Posts
    37

    Alternative to Inkey, Timer functions for auto log off

    Hi,

    I need to incorporate some vba code which will close my workbook if no key or mouse button has been pressed for about 5 minutes. I'm told that Inkey and Timer functions are not available in VBA. Is there any way to work around and solve this problem.

    Thanks in advance,
    Bala
    Last edited by Balachan56; 11-16-2009 at 07:58 AM.

  2. #2
    Forum Moderator dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003 & 2007
    Posts
    3,661

    Smile Re: Alternative to Inkey, Timer functions for auto log off

    Good afternoon Balachan56
    Quote Originally Posted by Balachan56 View Post
    I need to incorporate some vba code which will close my workbook if no key or mouse button has been pressed for about 5 minutes.
    Have a look at this post.

    HTH

    DominicB
    Now available : Ultimate Add-In 2007
    Integrates directly into the Office Excel Ribbon


    Download Ultimate Add-In v1.52 from www.dom-and-lis.co.uk
    90+ Utilities, 200+ Sub utilities last updated 25th April 2008
    Free!!

  3. #3
    Registered User
    Join Date
    09-19-2009
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: Alternative to Inkey, Timer functions for auto log off

    Thanks Dominic,

    Shall try the codes out (they look similar) and revert.

    Regds
    Bala

  4. #4
    Registered User
    Join Date
    09-19-2009
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: Alternative to Inkey, Timer functions for auto log off

    Hi Dominic,

    Both the codes turn off the workbook on expiry of the set duration, irrespective of whether entries are being made or not. I guess this is because there is no code in them to detect the keystrokes.

    I need a better alternative.

    Regds
    Bala

  5. #5
    Registered User
    Join Date
    09-19-2009
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: Alternative to Inkey, Timer functions for auto log off

    Hi,

    My basic idea is to close the workbook if there has been no entry made within the last 10 seconds. Each time an entry is made, the sheetchange procedure would be triggered, resulting in a different value for the downtime and hence BB. I find that the program gets hung at Closeapp() :If AA = BB line since on checking, BB is shown = 0. Earlier I used the syntax
    If Now= downtime .
    However, both ways I'm stuck. My use of the Public BB declaration does not seem to help. Has it been done wrongly? Should downtime be Public too?

    I tried some example code suggested by Dominic in this thread, but the ontime procedure just didn't detect workbook entries and closed down on expiry of the set duration.

    Can anyone suggest what's wrong and more importantly, a way out?

    Thanks in advance,
    Bala

    '''''''''''place in Thisworkbook
    Code:
    Public BB as string
    Private Sub Workbook_Open()        
    
    downtime = Now + TimeValue("00:00:10")
    BB = Str(downtime)
    
    Application.OnTime downtime, Procedure:="Closeapp", Schedule:=True
    End Sub
    
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
     
     downtime = Now + TimeValue("00:00:10")
     BB = Str(downtime)
     Application.OnTime downtime, Procedure:="closeapp", Schedule:=True
     
    End Sub
    '''''''''''place in Module

    Code:
    Public Sub closeapp()
    
    AA = Str(Now)
    AA = Str(downtime)
    If AA = BB Then
    ActiveWorkbook.Close
    End If
    
    End Sub
    Last edited by Balachan56; 11-16-2009 at 03:40 AM.

  6. #6
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240

    Re: Alternative to Inkey, Timer functions for auto log off

    hi Bala,

    Hi Rob,
    I need some help with the use of 'ontime' and the passing of the value of a variable from thisworkbook to a macro. I've posted the code and details on...
    Basically, I am trying to create and Auto log off option so that the workbook closes if no entry has been made for say 10 secs (5 mins in practice).
    I need to know 1. if my way of declaring a variable Public is correct or required at all.
    2.I need to know how to close any instance of 'ontime' that happens to be running, except the most recent one.
    Since I got only one reply, on posting this on the site and I'm not sure if its because I've posted in the wrong category (RB: I think you're in the right category), I'm contacting you directly for advice. Kindly view the thread mentioned above. Hope you can help.
    Thanks in advance,
    Bala
    I've spotted your visitor message to me & I can have a look after a small fix on your behalf...
    Can you please edit post 5 by adding the necessary code tags?

    Thanks
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  7. #7
    Registered User
    Join Date
    09-19-2009
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: Alternative to Inkey, Timer functions for auto log off

    Hi Rob,

    I'm not sure if this is what you want (Ref. edited post #5)

    Regds
    Bala

  8. #8
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240

    Re: Alternative to Inkey, Timer functions for auto log off

    hi Bala,
    Thanks for adding the correct code tags to post 5

    Here is an example file using VBA Noob's code from post 7 of http://www.excelforum.com/excel-prog...-workbook.html

    Download the file, open it & sit back & see what happens...

    Regarding your code & questions:
    - It's good practice to give your variables more meaningful names than "AA" & "BB".
    - 1) I'm not that good at understanding/explaining the scope of variables but this explanation of Chip's may help you: http://www.cpearson.com/excel/Scope.aspx
    (I should have a thorough read myself! )
    - 2) Check the VBE Help Files for "Ontime", Excel 2007 states:
    Syntax
    expression.OnTime(EarliestTime, Procedure, LatestTime, Schedule)
    expression A variable that represents an Application object.

    Parameters
    Name Required/Optional Data Type Description
    EarliestTime Required Variant The time when you want this procedure to be run.
    Procedure Required String The name of the procedure to be run.
    LatestTime Optional Variant The latest time at which the procedure can be run. For example, if LatestTime is set to EarliestTime + 30 and Microsoft Excel is not in Ready, Copy, Cut, or Find mode at EarliestTime because another procedure is running, Microsoft Excel will wait 30 seconds for the first procedure to complete. If Microsoft Excel is not in Ready mode within 30 seconds, the procedure won’t be run. If this argument is omitted, Microsoft Excel will wait until the procedure can be run.
    Schedule Optional Variant True to schedule a new OnTime procedure. False to clear a previously set procedure. The default value is True.
    hth
    Rob
    Attached Files Attached Files
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  9. #9
    Registered User
    Join Date
    09-19-2009
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: Alternative to Inkey, Timer functions for auto log off

    Hi Rob,
    Thanks for the vbe defns. I solved the problem with the following correction after removing all lines containing AA or BB.

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    Application.OnTime downtime, Procedure:="Closeapp", Schedule:=False
    downtime = Now + TimeValue("00:00:10")
    Application.OnTime downtime, Procedure:="closeapp", Schedule:=True

    End Sub

    This does not detect special function keys, however, it serves my purpose.

    Thanks
    Bala
    Last edited by Balachan56; 11-16-2009 at 07:58 AM.

  10. #10
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240

    Re: Alternative to Inkey, Timer functions for auto log off

    hi Bala,
    This does not take detect special function keys...
    What special function keys?

    Have you looked at the example file I uploaded?
    This file uses "Workbook_SheetCalculate" & "Workbook_SheetSelectionChange" subs to test more thoroughly for "inactivity".

    btw, if I have helped at all, please express thanks by clicking on the blue scales to add to my reputation - it is appreciated

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  11. #11
    Registered User
    Join Date
    09-19-2009
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: Alternative to Inkey, Timer functions for auto log off

    Hi Rob,

    I tried Van Noob's code 'as is' which means the sheet calculate etc., lines were implemented. But as explained earlier, it didn't help. The workbook closed within the first 'ontime' duration. During this period I clicked on several alpha numeric and function keys. Only F9 (Calc) seemed to have an effect. However, the shutdown period wasn't extended. Further, several nuisance pop ups arose. Anyway, as far as a true Inkey substitute is concerned, I'll cross that bridge when I come to it.

    Thanks,
    Bala

  12. #12
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240

    Re: Alternative to Inkey, Timer functions for auto log off

    okay, thanks for the feedback, marking as solved & adding to my rep

    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

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.2.0