Alert user when the user manually moves an email t

2020-05-03 10:11发布

问题:

I have found myself accidentally moving email items to an Archive folder instead of the standard folder in MS Outlook. For example, for an email address of Example@Email.Com, I open the Archive to locate an old email in folder "KeepThis" but then I forget to collapse the Archive folder and so I inadvertently move some other email message later on into the Archive/KeepThis folder when I really wanted to move it to the Example@Email.Com/KeepThis folder.

Is there a way to use VBA code to alert the user when an email item is manually moved into an Archive folder?

I tried unsuccessfully to modify the code by @thims at create-outlook-rule-which-runs-after-move-mail-to-specific-folder to show a msgbox, but could not figure out how to get it to trigger when I moved a mail item to any folder in either my main email folder nor in the Archive.

Here is what I've tried:

(1) I added the following code to ThisOutlookSession (obviously with my actual email address):

Public WithEvents FolderItems As Outlook.Items

Private Sub Application_Startup()
   Set FolderItems = Session.Folders("Example@Email.Com").Folders("Misc").Items
End Sub

Private Sub FolderItems_ItemAdd(ByVal Item As Object)
    MsgBox "ItemAdd event was triggered in folder Misc"
End Sub

That works if I move an email item into the "Misc" folder in my non-archived email account. Yay.

But, I cannot figure out how to set the FolderItems object to trigger when an email is moved to any folder.

(2) I can reference the general Archive folder by using:

Set FolderItems = Session.Folders("Archives").Folders("Misc").Items

But, that only triggers if I move something to the specific "Misc" folder in Archives and I want to trigger the event when an email is moved to any Archive folder, not just the "Misc" folder.

I've tried

Set FolderItems = Session.Folders("Archives").Items

but that does not work--there's no error, it just does not trigger when I move an email into any folder, nor does it trigger when a new folder is added/created in Archives; so, I'm not sure what will trigger with that code.

Thanks for any pointers to get things further along!

回答1:

Thanks @niton--apparently what I needed was for someone to tell me it was not possible! ;)

The code below will provide an alert whenever a file is moved to an Archive folder. (It will also provide an alert when any other change occurs too, so, it could get a little annoying for folks who are often messing with the Archive folders intentionally instead of unintentionally.)

I added the following code to the ThisOutlookSession Module and restarted Outlook and it works like a charm for me:

Public WithEvents myFolders As Outlook.Folders

Private Sub Application_Startup()
    Set myNS = Application.GetNamespace("MAPI")
    Set myFolders = myNS.Folders("Archives").Folders
End Sub

Private Sub myFolders_FolderChange(ByVal Folder As Outlook.Folder)
    'this triggers when a change occurs in the Archives folder (e.g., a new item is moved into or deleted from some folder in the Archive folder [often inadvertently])
    MsgBox "You have made a change in the Archives folder."
End Sub

In the final code I use, the msgbox will ask if I want to Undo the action. If I answer [Yes] then I use a SendKeys ^z (ctrl+z) to automate the undo, but I would love it if someone could tell me how to execute the Undo command using VBA, e.g., Application.Undo or something like that. I could not figure that out.

Thanks to @niton for pointing me to this post on Stackoverflow which had a comment by @Ryan Wildry which led me to this information on Microsoft describing how to use the FolderChange event along with some sample code which I modified to fit my needs.