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?
In VBA, you can access the Windows Event Logs to check for event IDs such as 1074 to determine the last shutdown time of your Windows computer. However, this would require using Windows Management Instrumentation (WMI) and querying the event logs, which can be a bit complex. Here's a general outline of how you can approach this:
Import the necessary libraries: To work with WMI in VBA, you'll need to reference the "Microsoft Scripting Runtime" and "Windows Script Host Object Model" libraries. To do this, open the Visual Basic for Applications (VBA) editor in Excel, go to "Tools" > "References," and check these libraries.
Write VBA code: Below is a sample VBA code snippet to retrieve the last shutdown time from the Windows Event Logs using WMI. This code assumes that you're looking for event ID 1074
This code queries the "System" log for events with EventID 1074 and retrieves the last shutdown time from the "TimeGenerated" property of the event. You can customize this code to suit your needs, such as saving the shutdown time to a variable or a cell in your Excel file.
Keep in mind that this code will require administrative privileges on your computer to access the Event Logs. Additionally, the actual event log location and structure may vary slightly depending on your Windows version. Make sure to test and adapt the code as needed for your specific environment.