有没有办法用VBA获取Windows最后一次关机时间?
我想跟踪工作时间的开始和结束。工作时间开始完成,指定的excel文件随windows启动,宏自动运行。但关机时间看起来有点困难。
如果我是对的:事件 ID 1074 - 此事件在两种情况下被记录:通过“开始”菜单中的关闭命令或当应用程序导致计算机重新启动或关闭时。
是否可以用VBA检查它或者还有其他选项吗?
在 VBA 中,您可以访问 Windows 事件日志来检查事件 ID(例如 1074),以确定 Windows 计算机的上次关闭时间。但是,这需要使用 Windows Management Instrumentation (WMI) 并查询事件日志,这可能有点复杂。以下是如何实现这一目标的总体概述:
导入必要的库: 要在 VBA 中使用 WMI,您需要引用“Microsoft 脚本运行时”和“Windows 脚本宿主对象模型”库。为此,请在 Excel 中打开 Visual Basic for Applications (VBA) 编辑器,转到“工具”>“引用”,然后检查这些库。
编写VBA代码: 下面是一个示例 VBA 代码片段,用于使用 WMI 从 Windows 事件日志中检索上次关闭时间。此代码假设您正在查找事件 ID 1074
Sub GetLastShutdownTime()
Dim objWMIService As Object
Dim colEvents As Object
Dim objEvent As Object
' Connect to WMI service
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
' Query for events with EventID 1074 (shutdown events)
Set colEvents = objWMIService.ExecQuery("SELECT * FROM Win32_NTLogEvent WHERE LogFile='System' AND EventCode=1074")
' Iterate through the events to find the last shutdown
For Each objEvent In colEvents
' The last shutdown time is in the 'TimeGenerated' property
MsgBox "Last Shutdown Time: " & objEvent.TimeGenerated
Exit For ' Exit after the first matching event (last shutdown)
Next objEvent
' Clean up
Set objWMIService = Nothing
Set colEvents = Nothing
Set objEvent = Nothing
End Sub
此代码在“系统”日志中查询 EventID 1074 的事件,并从该事件的“TimeGenerate”属性中检索上次关闭时间。您可以自定义此代码以满足您的需求,例如将关闭时间保存到 Excel 文件中的变量或单元格中。
请记住,此代码需要您计算机上的管理权限才能访问事件日志。此外,实际事件日志位置和结构可能会略有不同,具体取决于您的 Windows 版本。确保根据您的特定环境的需要测试和调整代码。
请使用下一个函数,能够返回最后一次关机时间作为本地时间。该代码实际上需要引用“Microsoft WMI Scripting V1.2 Library”。它可以在没有它的情况下运行,声明所有涉及的变量/对象
As Object
(后期绑定)。但是添加相应的参考可以提供智能感知建议,并且可以用于更多任务:
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
如果添加所需的引用看起来很困难,我可以放置一段能够自动添加它的代码。事实上,我现在就发布它:
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
上面的功能可以使用下一个Sub来测试:
Sub TestGetLastShutdownTime()
Debug.Print GetLastShutdownTime
End Sub
请在测试后发送一些反馈。