This question is linked to the article Windows Management Instrumentation (WMI) Tutorial.
I was not able to get this code to work. Or more precisely I could not get it to identify event 4. I could get events 7 and 18 but not 4.
I was particularly interested in closing out the application if the computer goes into sleep mode.
I copied the code exactly but I added debug.print so I could know when it is initialized and the power eventtype.
The sub sink_OnObjectRe ady is never called and the event type is never printed in debug...
I was not able to get this code to work. Or more precisely I could not get it to identify event 4. I could get events 7 and 18 but not 4.
I was particularly interested in closing out the application if the computer goes into sleep mode.
I copied the code exactly but I added debug.print so I could know when it is initialized and the power eventtype.
The sub sink_OnObjectRe ady is never called and the event type is never printed in debug...
Code:
Option Compare Database Option Explicit 'This is to allow the variable sink to raise events Dim WithEvents sink As SWbemSink Private Sub sink_OnObjectReady(ByVal objWbemObject As SWbemObject, ByVal objWbemAsyncContext As SWbemNamedValueSet) 'Event Type 4 is the code for entering sleep mode Debug.Print "EventType: " & objWbemObject.EventType If objWbemObject.EventType = 4 Then Application.Quit End If End Sub Private Sub Class_Initialize() 'Creates a new WMI object Dim services As SWbemServices 'Creates the callback object Set sink = New SWbemSink 'Gets the WMI service. The period in the string is used to reference the local computer. 'You may use an IP address if you wish to query a remote computer. Set services = GetObject("winmgmts:\\.\root\cimv2") 'Executes an asynchronous notification query services.ExecNotificationQueryAsync sink, "Select * from Win32_PowerManagementEvent" Debug.Print "Initialized." End Sub
Comment