Excel

Exit Windows - (Log-Off, Reboot, or Shut Down)

Ease of Use

Intermediate

Version tested with

2000 

Submitted by:

johnske

Description:

Ever wonder how your PC gets re-booted automatically when you install something? It's done with a simple API call in earlier Windows systems and with a shell function in XP - here's how... 

Discussion:

The uses for any or all of these are up to you. For instance, you may want to shut down your PC after an open workbook has undergone a period of inactivity, or, you may want to log off if someone gives an incorrect password...(etc.). An example of this is given here > http://www.vbaexpress.com/kb/getarticle.php?kb_id=516 ***NOTE: As most of these commands closes the current Windows log-in session, it is possible that the closure could cause loss of data or data corruption in some open applications. This decision to use these is yours alone, hence - YOU USE AT YOUR OWN RISK! REFER TO OUR TERMS OF USE OR A MODERATOR FOR ANY QUESTIONS!.*** 

Code:

instructions for use

			

Option Explicit Declare Function ExitWindowsEx& Lib "user32" _ (ByVal uFlags&, ByVal wReserved&) '//constants needed to exit Windows Global Const EWX_LOGOFF = 0 Global Const EWX_SHUTDOWN = 1 Global Const EWX_REBOOT = 2 Global Const EWX_FORCE = 4 '*******************SHUT DOWN********************* Sub TurnOffXP() 'shutdown.exe will NOT work on Windows 95, 98, 98 SE and ME ActiveWorkbook.Save Application.DisplayAlerts = False Application.Quit '//shut down the computer Shell "shutdown -s -t 02", vbHide End Sub Sub TurnOffPC() Dim Action As Long ActiveWorkbook.Save Application.DisplayAlerts = False '//shut down the computer Action = ExitWindowsEx(EWX_SHUTDOWN, 0&) Application.Quit End Sub '*************************************************** '*********************REBOOT*********************** Sub RebootXP() 'shutdown.exe will NOT work on Windows 95, 98, 98 SE and ME ActiveWorkbook.Save Application.DisplayAlerts = False Application.Quit '//re-boot the computer Shell "shutdown -r -t 02", vbHide End Sub Sub ReBootPC() 'will not work with XP Dim Action As Long ActiveWorkbook.Save Application.DisplayAlerts = False '//re-boot the computer Action = ExitWindowsEx(EWX_REBOOT, 0&) Application.Quit End Sub '*************************************************** '*********************LOG OFF*********************** Sub LogOffXP() 'this will NOT work on Windows 95, 98, 98 SE and ME ActiveWorkbook.Save Application.DisplayAlerts = False Application.Quit '//log-off Shell "shutdown -l -t 02", vbHide 'Ok End Sub Sub LogOffPC() 'this will also work with XP Dim Action As Long ActiveWorkbook.Save Application.DisplayAlerts = False '//log-off Action = ExitWindowsEx(EWX_LOGOFF, 0&) Application.Quit End Sub '*************************************************** '**********************FORCE************************ Sub ForceRebootXP() 'this will NOT work on Windows 95, 98, 98 SE and ME ActiveWorkbook.Save Application.DisplayAlerts = False Application.Quit '//force Shell "shutdown -r -f -t 02", vbHide End Sub Sub ForceRebootPC() 'this also works with XP '//emergency shut down => log-off, don't save Dim Action As Long ActiveWorkbook.Save Application.DisplayAlerts = False '//force Action = ExitWindowsEx(EWX_FORCE, 0&) Application.Quit End Sub '***************************************************

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Insert/Module
  4. Copy and paste the code into the Module
  5. Now select File/Close and Return To Microsoft Excel
  6. Save the workbook
 

Test the code:

  1. An example to experiment with is included in the zip attachment
  2. NOTE: Running these macros as is will reboot, shut-down (etc.) without further warning
 

Sample File:

ExitWindows.zip 13.06KB 

Approved by mdmackillop


This entry has been viewed 248 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2014 VBA Express