简体   繁体   中英

How to force restart PC remotely using Outlook 365 VBA macro by sending an email to myself with specific subject keyword?

Please note that below code was found online; however when tested, it did not work. Please can you arrange to fix below code or provide an alternative code: Please note that creating a rule and running a script is not available and modification in registry is not possible.

Option Explicit

Private Declare PtrSafe Function LockWorkStation Lib "user32.dll" () As Long

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)

 Dim EntryID

 Dim lastItem

 EntryID = Split(EntryIDCollection, ",")

 For i = 0 To UBound(EntryID)

    Set lastItem = Application.Session.GetItemFromID(EntryID(i))

    If (LCase(lastItem.Subject) = "shutdown") Then

        Call Shell("Shutdown /s")

    End If

    If (LCase(lastItem.Subject) = "logoff") Then

        Call Shell("Shutdown /l")

    End If

    If (LCase(lastItem.Subject) = "restart") Then

        Call Shell("Shutdown /r")

    End If

    If (LCase(lastItem.Subject) = "lock") Then

        a = LockWorkStation()

    End If

 Next

End Sub

Thanks

Most probably your NewMailEx event handler is not fired when new items arrive to the Inbox. You can't just paste the code from the online resource and get it working out of the box for Outlook events. To subscribe to the event in the Outlook VBA editor you need to choose the source object (in your case that is Application ) then on the right hand-side drop-down list you may find the list of available events. Choosing the event from the list you will get the event handler generated in the code. Then you can paste the code to the generated event handler.

Be aware, the EntryIDsCollection string contains the Entry ID that corresponds to that item. Note that this behavior has changed from earlier versions of the event when the EntryIDCollection contained a list of comma-delimited Entry IDs of all the items received in the Inbox since the last time the event was fired. Now the NewMailEx event is fired once for every received item that is processed by Microsoft Outlook.

Also pay attention to the fact that items are processed only when Outlook is run. So, if you want to shut down the machine by receiving an email you need to keep Outlook running on the system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM