0

I would like to run a macro just before Windows shutsdown to log the time. I wrote below program to run the macro just right after windows starts up. I will put the shortcut in Windows startup folder so that when windows starts, it will log the current day time and date.

But how to run a macro just before shutting down the PC. The same below code will work for Start up and shut down with little change. But how to run the macro or code before windows turns off

Private Sub Workbook_Open()
'
' Macro1 Macro
'
Dim Day As Date
Dim Tim As Date
Dim Row As Integer
Dim Col As Integer
Row = 1
Col = 1

Day = DateValue(Now)
Tim = TimeValue(Now)
While (Cells(Row, Col) <> "")
Row = Row + 1
Wend
Cells(Row, Col) = Day
Cells(Row, Col + 1) = Tim

ThisWorkbook.Save

'
End Sub
Community
  • 1
  • 1
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
  • 1
    Take a look at this answer http://stackoverflow.com/questions/101647/how-to-schedule-a-task-to-run-when-shutting-down-windows – kamjagin Jul 09 '13 at 15:37

1 Answers1

3

If you put something in the startup folder, it will not really run at startup, but at user logon.

To run something at startup or shutdown, you need to implement startup or shutdown scripts through Group Policy. Type "gpedit.msc" in the run box to explore Group Policy.

Another solution is the system eventlog, events from source "eventlog" are logging all startup and shutdown activity.

Martin Binder
  • 1,066
  • 7
  • 5
  • +1 for you. but look at the provided solution(code) for logging. do you really think he knows what Group Policy or system eventlog is? he is starting an excel sheet to log 'events' lol –  Jul 09 '13 at 15:52
  • Maybe no ;-)) But doing an excerpt from the system eventlog just to grab shutdowns and startups should be quite straight forward. – Martin Binder Jul 11 '13 at 17:01