macro to shut down laptop (1 Viewer)

smiler44

Registered User.
Local time
Today, 13:58
Joined
Jul 15, 2008
Messages
641
I have tried various macros from the internet but none have worked.
can you help with vba code to shut down a windows 7 laptop and a 8 laptop?

thank you in advance

smiler44
 

isladogs

MVP / VIP
Local time
Today, 13:58
Joined
Jan 14, 2017
Messages
18,186
Just tested the code in that link.
The single and double quotes are not standard so the code needs amending to

Code:
Option Compare Database
Option Explicit

' ‘*******************SHUT DOWN*********************
Public Function TurnOff()
    Shell "shutdown -s -t 02", vbHide
End Function
 
' ‘*********************REBOOT***********************
Public Function Reboot()
    Shell "shutdown -r -t 02", vbHide
End Function
 
' ‘*********************LOG OFF***********************
Public Function LogOff()
    Shell "shutdown -l -t 02", vbHide
End Function
 
 '‘**********************FORCE************************
Public Function ForceReboot()
    Shell "shutdown -r -f -t 02", vbHide
End Function

You may only need the first function 'TurnOff' which I've tested and it works in Win10. I see no reason why it wouldn't work in earlier versions of Windows

BTW it works with immediate effect and no warnings. Make sure everything is saved before the code runs
 
Last edited:

smiler44

Registered User.
Local time
Today, 13:58
Joined
Jul 15, 2008
Messages
641
isladogs,
thank you for the code, it has worked.

I found code on the internet but the first line of code was application.quit,
so excel did just that. Even when i removed that line of code the rest did not work. Thank you very much for your quick and accurate working reply.

smiler44
 

isladogs

MVP / VIP
Local time
Today, 13:58
Joined
Jan 14, 2017
Messages
18,186
You're very welcome ...but I'm glad you came back with a comment about Application.Quit as there's something I forgot to mention

Using that code will close your Access application immediately but won't run any subsequent code to close Windows etc.

Using the code I supplied will not only shut down your PC but of course will terminate all running applications. The problem with that is that forcibly closing Access (and some other apps) without shutting it down 'nicely'' can cause corruption and instability.

So I recommend strongly the following if you use this code
1. Make sure no other apps are running
2. Add the line Application.Quit after the Shell line

I've tested this for the TurnOff function.
Access closes normally then Windows shuts down. SUCCESS.

I haven't tested the other functions but suggest adding the same line to each
 

smiler44

Registered User.
Local time
Today, 13:58
Joined
Jul 15, 2008
Messages
641
thanks for the heads up. i had not intended to bother closing any open programs, probably did not think it necessary so will do as you advise.

if the shell line is read, will the application.quit be read or will the laptop have closed down before the line is read and executed or will excel close while the lap top is shutting down?

smiler44
 

isladogs

MVP / VIP
Local time
Today, 13:58
Joined
Jan 14, 2017
Messages
18,186
I've only tested it from Access and the application.quit line is read before shutting down.
Suggest you try it from Excel on a test file - it should be fine AFAIK.
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:58
Joined
Sep 21, 2011
Messages
14,042
Just extend the timeout?

https://www.lifewire.com/shutdown-command-2618100

thanks for the heads up. i had not intended to bother closing any open programs, probably did not think it necessary so will do as you advise.

if the shell line is read, will the application.quit be read or will the laptop have closed down before the line is read and executed or will excel close while the lap top is shutting down?

smiler44
 

smiler44

Registered User.
Local time
Today, 13:58
Joined
Jul 15, 2008
Messages
641
i used this code

sorry i have forgotten the correct way to paste code into a post

Private Sub shutdown()

Shell "shutdown -s -t 05", vbHide
Application.Quit
End Sub


when i rebooted and opened Excel, I think it showed that it had closed as if application.quit had not been read.

should i replace t with f a per the page Gasman has given? A very interesting page but my skill level makes it hard for me to understand, sorry

the web page Gasman has given shows /f and /t does / or - make a difference?

smiler44
 

isladogs

MVP / VIP
Local time
Today, 13:58
Joined
Jan 14, 2017
Messages
18,186
when i rebooted and opened Excel, I think it showed that it had closed as if application.quit had not been read.

How would you know this after rebooting?

should I replace t with f a per the page Gasman has given? A very interesting page but my skill level makes it hard for me to understand, sorry

the web page Gasman has given shows /f and /t does / or - make a difference?

Each of the switches do different things
The t switch indicates the time delay in seconds - if omitted a 30 second delay is applied.
The f switch is used to force apps to close without warning. Is that what you want?

In Excel, remove the code line Option Compare Database

I've now tested all 4 functions in both Excel & Access
All work perfectly with the exception of logoff which just closes the application (due to Application.Quit). I'll keep experimenting with that one.

It makes no difference whether you use '-' or '/'
Experimenting with different time intervals suggests 2 or 3 works fine for me. It doesn't matter whether you include the leading zero or not. i.e. 2 or 02 both fine

As for your other question, use the code tag button (#) in the toolbar above the post window

HTH
 

isladogs

MVP / VIP
Local time
Today, 13:58
Joined
Jan 14, 2017
Messages
18,186
OK - solved the logoff function issue
It only works for me if you remove the /t switch

The following code has been fully tested using Windows 10 in both Access & Excel

Code:
Option Compare Database[COLOR="SeaGreen"] 'omit this line in Excel[/COLOR]
Option Explicit

[COLOR="seagreen"]'NOTE switches used below
'/s = shutdown
'/r = reboot
'/l = logoff
'/f = force apps to close without warning
'/t xxx = time delay of xxx seconds e.g. /t 2[/COLOR]

' ‘*******************SHUT DOWN*********************
Public Function TurnOff()
    Shell "shutdown /s /t 2", vbHide
    Application.Quit
End Function
 
' ‘*********************REBOOT***********************
Public Function Reboot()
    Shell "shutdown /r /t 2", vbHide
    Application.Quit
End Function
 
' ‘*********************LOG OFF***********************
Public Function LogOff()
   [COLOR="seagreen"] 'omit /t switch or it doesn't work
   ' Shell "shutdown /l /t 2", vbHide[/COLOR]
    Shell "shutdown /l", vbHide
    Application.Quit
End Function
 
 '‘**********************FORCE************************
Public Function ForceReboot()
    Shell "shutdown /r /f /t 2", vbHide
    Application.Quit
End Function
 
Last edited:

smiler44

Registered User.
Local time
Today, 13:58
Joined
Jul 15, 2008
Messages
641
isladogs, just tried again and excel opened as if it had been closed properly. it could have been that last time i was stepping through the code using the f8 button.

got to be honest i had not seen the line Option Compare Database so had not included it

what i'm looking to do.
i will either play songs randomly selected by excel or select a load of songs to play.
Both ways will use windows media player

i want to shut the laptop down after so many minutes as i will have gone to bed.

not sure which to use, have the laptop check how many minutes have lapsed since i started the macro or add up the run time of each music file that is played and once x minutes of music have been played, to then shut down the laptop.

i guess i will also have to quit windows media player as well

smiler44
 

smiler44

Registered User.
Local time
Today, 13:58
Joined
Jul 15, 2008
Messages
641
Access is beyond me.
I also use Excel as an alarm clock, it plays a song at a preset time.

i have spent time searching how to close windows media player. cant find anything that works.... any ideas?

smiler44
 

isladogs

MVP / VIP
Local time
Today, 13:58
Joined
Jan 14, 2017
Messages
18,186
Sorry I no longer use WMP. It's ancient technology and IIRC no longer supplied with Windows 10.

You can also use Access as an alarm clock, stop watch, event timer etc etc
 
Last edited:

smiler44

Registered User.
Local time
Today, 13:58
Joined
Jul 15, 2008
Messages
641
the advice and help given in shutting down the lap top is appreciated thank you
as I can not shut the laptop down

smiler44
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:58
Joined
Sep 21, 2011
Messages
14,042
As WMP is just a media player and not altering any files, I'd just shut the computer down regardless.?


My win10 systems have developed an awful habit of restoring open apps when shutting down normally, bit like sleep mode?, so if WMP does not start up on swicthing back on, I might well start using a batch file to shut the computer down. :)
 

Users who are viewing this thread

Top Bottom