Shut down a computer from within vba

drom

Active Member
Joined
Mar 20, 2005
Messages
441
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
  6. 2007

hi and many thanks in advance!



is there a way to shut down a computer from within vba

to quit excel we use:

Code:
Application.Quit

1) to quit a computer?

thanks!
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
How about this?:

Code:
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiExitWindowsEx Lib "user32" _
        Alias "ExitWindowsEx" _
        (ByVal uFlags As Long, ByVal dwReserved As Long) _
        As Long

Public Const EWX_FORCE = 4      'Forcibly terminates processes
                                'that do not respond.
Public Const EWX_LOGOFF = 0     'Terminates processes, then logs off.
Public Const EWX_SHUTDOWN = 1   'Powers the system off, if possible.
Public Const EWX_REBOOT = 2     'Reboots the system.

Function fTerminateWin(lngExitVal As Long)
    fTerminateWin = apiExitWindowsEx(lngExitVal, 0)
End Function

Sub test()
    fTerminateWin (4)           'Only 4 and 0 work on my machine
End Sub

HTH
 

Watch MrExcel Video

Forum statistics

Threads
1,119,139
Messages
5,576,308
Members
412,716
Latest member
thviid
Top