PDA

View Full Version : [SOLVED:] Capture emails which are moved



aravindhan_3
02-24-2011, 05:52 AM
Hi,

Is there a macro which can capture the subject, email address & moved time.

What i do now is, I get emails on my generic email box. After some time I will move/allot the email to the sub folders. Now i have the email recieved time, subject etc but not sure how to capture the details of the emails I moved.

somethign like, email recd date, from, subject, moved date, moved folder name.

thanks for your help
Arvind

JP2112
02-24-2011, 07:42 AM
How many subfolders are involved? If it's only one or two, you should be able to adapt this code:

Private WithEvents Items1 As Outlook.Items
Private WithEvents Items2 As Outlook.Items

Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
' change these lines to point to the subfolders you are moving emails to
Set Items1 = objNS.GetDefaultFolder(olFolderInbox).Folders("SubFolder 1").Items
Set Items2 = objNS.GetDefaultFolder(olFolderInbox).Folders("SubFolder 2").Items
End Sub

Private Sub Items1_ItemAdd(ByVal item As Object)

On Error Goto ErrorHandler

Dim Msg As Outlook.MailItem

If TypeName(item) = "MailItem" Then
Set Msg = item
' change this to do what you want with the information, i.e. generate a new email, write info to spreadsheet, etc
Debug.Print Msg.ReceivedTime & " " & Msg.SenderName & " " & Msg.Subject & " " & Format(Now, "mm/dd/yyyy") & " " & Items1.Parent.Name

End If

ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub

Private Sub Items2_ItemAdd(ByVal item As Object)

On Error Goto ErrorHandler

Dim Msg As Outlook.MailItem

If TypeName(item) = "MailItem" Then
Set Msg = item
' change this to do what you want with the information, i.e. generate a new email, write info to spreadsheet, etc
Debug.Print Msg.ReceivedTime & " " & Msg.SenderName & " " & Msg.Subject & " " & Format(Now, "mm/dd/yyyy") & " " & Items2.Parent.Name

End If

ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub

aravindhan_3
02-25-2011, 06:26 AM
Hi

thanks for the code, however when i copy this into a module and changed the folder name. But nothing happens neither i get error nor the result.

can you please let me know how do i do this? how do i get the report? in excel?

JP2112
02-25-2011, 06:54 AM
These are event handlers. The code should be placed in ThisOutlookSession, then you must restart Outlook.

Also, any time you make any changes to event handlers, you have to restart Outlook.

aravindhan_3
02-25-2011, 10:53 PM
Hi Thanks again,

I did this, but where the result is captured? do I have to add user-defined fields? or how to do i get this to excel?

Please help me as this is the first time i am trying macro with outlook.

JP2112
02-26-2011, 06:46 PM
It's being printed to the Immediate Window. You'll need to adapt the code to do with the information whatever you want.

aravindhan_3
02-28-2011, 09:51 PM
Thanks,

Can you please help me to capture this in a excel?

JP2112
03-01-2011, 12:53 PM
Here's an example you can adapt:

http://www.codeforexcelandoutlook.com/blog/2009/03/extract-calendar-data-from-outlook-redux/

DawnTreader
04-21-2015, 10:21 AM
Hello all

I know it has been a while but I need to ask a question about this thread.

I would like to run this against the mainstore of my outlook account so that anytime an email from the inbox is moved to any other mail folder it will be tagged with a date. this code looks like it would do the job, but I have more than 2 folders that I want to "watch". in fact I need to watch any folder that holds mail items, especially if a new folder is created.

Another problem is the fact that these emails might not be moved within the current open outlook session on my computer but might be moved by someone else who has been given full access through exchange server. the situation is that we have a "pool" address that receives all of our service email and is delegated to various people. when someone is given the task it moves to a "My_tasks\persons name" folder. when they complete it they move it to a "completed\country" folder.

I was thinking that if I put the "watch" on the mainstore that might work, I haven't tried it yet, I thought I would get this post in so that I could get feedback on the idea asap as I am on a time crunch with the code.

any and all help is appreciated.