Save, Close, Quit & Reboot

dgr

Board Regular
Joined
Apr 24, 2005
Messages
176
Hi,
I'm using Excel 2013 on Windows 8.

As per the title of this thread, I want to save a workbook, wait for a few seconds, close the workbook, quit Excel & reboot my pc. I'm using the following macro which does not work because Windows refuses to shutdown saying that "cannot quit Excel".

Could you show me a macro that works? Thanks.
Code:
Sub shutdown()
ActiveWorkbook.Save
Application.Wait (Now + TimeValue("0:00:05"))
Application.DisplayAlerts = False
Application.Quit
Shell "C:\Windows\System32\shutdown.exe /r /t 0", vbHide 'reboot
End Sub

Interestingly, if I run this code on a small workbook, it works but not on a big workbook...
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Thanks for your reply. Nope, that did not help. I increased the time value to 10 seconds. Any other ideas? Thanks.
 
Upvote 0
That shouldn't work regardless of the workbook size:

Code:
Sub shutdown()
ActiveWorkbook.Save
Application.Wait (Now + TimeValue("0:00:05"))
Application.DisplayAlerts = False
Application.Quit
Shell "C:\Windows\System32\shutdown.exe /r /t 0", vbHide 'reboot <-- This comes after the application has quit, it is no longer running so will not see  this line to execute.
End Sub

I would imagine you can set the saved flag to true then just run the shutdown without the quit, the app will quit on the shutdown
 
Upvote 0
Thanks for your reply Blade. I tried the following code based on my understanding of your explanation. While the rebooting took place smoothly, Excel's Document Recovery kicked in after the reboot claiming that my original file was not saved. Is there a more elegant solution you could think of? Thanks.
Code:
Sub shutdown()
ActiveWorkbook.Saved = True
Application.Wait (Now + TimeValue("0:00:05"))
Application.DisplayAlerts = False
Shell "C:\Windows\System32\shutdown.exe /r /t 0", vbHide 'reboot
End Sub
 
Upvote 0
This will fix that just before the reboot:

activeworkbook.Saved = true

edit: Oh wait, I just saw you did that already.

Not sure what makes this happen then.
 
Upvote 0

Forum statistics

Threads
1,216,743
Messages
6,132,459
Members
449,729
Latest member
davelevnt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top