+ Reply to Thread
Results 1 to 18 of 18

VBA: Prevent Machine from Entering Sleep/Standby Mode

  1. #1
    Forum Contributor
    Join Date
    08-04-2011
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2007
    Posts
    109

    VBA: Prevent Machine from Entering Sleep/Standby Mode

    Looking for a VBA script that will prevent my machine from entering sleep or standby mode. My IT department has prevented the ability to tweak these settings. I also do not have admin rights to download an app such as Caffeine that can do this. My thought is to create a loop that uses the SendKeys function and then waits 5-10 minutes before running back through the loop. I am not sure SendKeys is enough to prevent sleep mode. Any ideas would be appreciated.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    <rant>
    1. The IT Department has instituted a PC sleep/standby policy. This is likely for two reasons:
    a. Save energy for the company to reduce costs
    b. Security, so nobody can walk up to your PC while you're away and use it. In some businesses this is a requirement (PCI, PA-DSS, etc.) to protect customers and confidential/personal information.

    2. Your organization probably has a policy stating that you must comply with IT Security policies, and that circumventing such policies could result in punishment (write-up, warning, termination, etc).

    3. You don't have admin rights for a good reason. Companies don't like giving the keys to the kingdom to those that don't know how to run or protect a kingdom. It's far less likely that you'll bring down the kingdom with non-admin permissions.

    4. I'm not saying that I'm a King or the company is my Kingdom, but I'd never permit this type of work-around.
    </rant>

  3. #3
    Forum Contributor
    Join Date
    08-04-2011
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2007
    Posts
    109

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    Thanks Paul.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    Hello Dionysos,

    This routine will prevent the computer from going into power save mode until Excel is closed. Copy and paste this code into a new VBA module in your workbook. Run the macro KeepPowerOn. This will not prevent the screen saver from coming up, only prevent the computer from going into sleep mode and turning off the display. Doubtful this will cause any problems for you.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Contributor
    Join Date
    08-04-2011
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2007
    Posts
    109

    [SOLVED] Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    Nice job Leith.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    Hello Dionysos,

    Glad I could help. Are you running Windows 7?

  7. #7
    Registered User
    Join Date
    06-05-2012
    Location
    sometimes ioefeen
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    Found this answer from a google search - thanks.

    I am hoping to deploy this from Access to prevent sleep if laptop screen is closed; will this work in Win7?

  8. #8
    Registered User
    Join Date
    05-21-2013
    Location
    Portugal
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    I did try it in win7 and did work

    thanks

  9. #9
    Registered User
    Join Date
    04-16-2012
    Location
    Switzerland
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    Dear Leith,
    Thanks for this macro.
    I am a beginner in Macros and have a question.

    I am running a macro, that is updating worksheets from a web application.
    Overall the whole process takes about 50 mins to 1 hr.
    I would like to run the macro in the evening and then once I am back in the morning, the files should be updated.
    The problem is my laptop goes to sleep in 30 mins if there is no activity.

    How can I incorporate your macro into my existing macro, so that once the sheets are updated, the excel closes and the laptop can go to sleep?

    Many thanks for your help.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    Hello howwzat,

    Add a new module to your workbook's VBA project and paste the code from post #4 into it.

    At the start of your macro add the line of code below:
    Please Login or Register  to view this content.
    Once you close Excel, the normal power management will be restored.

  11. #11
    Registered User
    Join Date
    08-22-2012
    Location
    Chaska, MN
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    This is a fantastic solution. Thanks to you wise people many years ago that posted it. Is there a similar VBA solution that also prohibits the screensaver from launching?

  12. #12
    Registered User
    Join Date
    05-21-2013
    Location
    Portugal
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    if you make the mouse move everytime that you use this function your screensaver shouldn't launch

    something like this:
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    08-22-2012
    Location
    Chaska, MN
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    I found a very nice solution here that seems to work exactly as I had hoped. By putting calling this in Private Sub Workbook_Open()
    , it disables screensaver when this Excel file is open and when Excel is closed, everything reverts to as it was before.

    rfbgb, I had read that programmatic cursor jiggling didn't actually stop screensavers, so I never tried those solutions. If this below doesn't continue to work, I'll try your suggestion next. Thanks for the thought you put into it.

    From http://www.ozgrid.com/forum/showthre...681#post373681

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    07-31-2004
    Posts
    1

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    Pardon my late jump-in.
    This looks like what I am after - thank you.

    As it errs at '...ligar...'
    What does that mean, and how to address/correct the error please?

    Please Login or Register  to view this content.
    Thank you,

    Stefan

  15. #15
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,602

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    sgh,
    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    Ben Van Johnson

  16. #16
    Registered User
    Join Date
    05-21-2013
    Location
    Portugal
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    Ligar is a variable from my code, you can remove it from the code if you want that. Then every time you call the function the computer will stay wake

  17. #17
    Registered User
    Join Date
    10-31-2014
    Location
    us
    MS-Off Ver
    xp
    Posts
    1

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    I'm running Windows 7 with Office XP. I have some excel macros that can run for a long time and put my pc to sleep which is very annoying. I don't want to change my power settings because i don't know when it will take a long time so I'd like to automate this. I tried your code but it doesn't work.

    I ran the KeepPowerOn macro step by step and the calls to setthreadexecutionstate just returns 0s.


    I have kernel32.dll in my System32 folder.

  18. #18
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,602

    Re: VBA: Prevent Machine from Entering Sleep/Standby Mode

    alvinenns,
    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.

+ 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