-1

Is there a way to get the last shutdown time of Windows with VBA?

I would like to follow the start and the end of the working time. The start of the working time is done, the specified excel file starts with windows and the macro runs automatically. But the shutdown time looks bit difficult.

If I am right: Event ID 1074 - this event is logged in two situations: either by a shutdown command from the Start menu or when an application causes the computer to restart or shutdown.

Is it possible to check it with VBA or is there any other options?

1
  • Please, try using the function I provided in my answer and send some feedback. Commented Oct 6, 2023 at 12:56

1 Answer 1

0

Please, use the next function, able to return the last ShutDown time as Local time. The code, as it is, needs a reference to 'Microsoft WMI Scripting V1.2 Library'. It can be run without it, declaring all involved variables/objects As Object (Late binding). But adding the respective reference offers intellisense suggestions and it can be used for more tasks:

Function GetLastShutdownTime() As Date
    'It needs a reference to 'Microsoft WMI Scripting V1.2 Library'
    Dim DateTime As WbemScripting.SWbemDateTime  'Object
    Dim objWMIService As WbemScripting.SWbemServicesEx  'Object
    Dim colEvents As WbemScripting.SWbemObjectSet ' Object
    Dim objEvent As WbemScripting.SWbemObjectEx ' Object
    
    ' Get WMI service object:
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    
    ' Create a new datetime object.
    Set DateTime = CreateObject("WbemScripting.SWbemDateTime")
    
    ' Query for events having  EventCode=1074 (shutdown events). They are returnded from the last one to the first:
    Set colEvents = objWMIService.ExecQuery("SELECT * FROM Win32_NTLogEvent WHERE LogFile='System' AND EventCode=1074")
    
    ' Iterate through the events to find last shutdown (the first one found):
    For Each objEvent In colEvents
        Debug.Print objEvent.timegenerated 'it rturns the last shutDown as GMT (string) time!
        
        DateTime.value = objEvent.TimeGenerated 'place the GMT found time as DateTime value
        
        'return the event date time as LOCAL TIME:
        GetLastShutdownTime = DateTime.GetVarDate: Exit For
    Next objEvent
    
    ' Clean up memory
    Set objWMIService = Nothing: Set colEvents = Nothing: Set objEvent = Nothing: Set DateTime = Nothing
End Function

If adding the required reference looks difficult, I can place a piece of code able to add it automatically. In fact, I will post it now:

Private Sub WVIScriptingFromFile()
  'Microsoft WMI Scripting V1.2 Library'...
   On Error Resume Next
    ThisWorkbook.VBProject.References.AddFromFile "c:\Windows\System32\wbem\wbemdisp.TLB"
    If Err.number <> 0 Then
        MsgBox "The reference already exists..."
    Else
        MsgBox "The reference added successfully..."
    End If
  On Error GoTo 0
End Sub

The above function can be tested using the next Sub:

Sub TestGetLastShutdownTime()
   Debug.Print GetLastShutdownTime
End Sub

Please, send some feedback after testing it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.