Hi All,
I work for a organisation where we have multiple mailboxes mapped to microsoft outlook.
I would like to know whether it is possible to create a macro which should capture the time of receipt of the email, subject line and the senders information in Excel (email received in secondary mailbox not the primary one)
Vice Versa Macro should capture the time of email sent, senders name, receipent name and subject (which we sent from Secondary Mailbox to outside world)
Any help in this regard will appreciated
Thanks-Kamal
The following macro will create a list in the active worksheet of emails received from the specified subfolder...
Option Explicit Sub test() Dim olApp As Object Dim olNS As Object Dim olFldr As Object Dim olMail As Object Dim arrData() As Variant Dim Cnt As Long Set olApp = CreateObject("Outlook.Application") Set olNS = olApp.GetNamespace("MAPI") ' Change the name of the subfolder accordingly Set olFldr = olNS.GetDefaultFolder(olFolderInbox).Folders("MySubFolder") Cnt = 0 For Each olMail In olFldr.Items Cnt = Cnt + 1 ReDim Preserve arrData(1 To 3, 1 To Cnt) arrData(1, Cnt) = olMail.Sender arrData(2, Cnt) = olMail.Subject arrData(3, Cnt) = olMail.ReceivedTime Next With ActiveSheet.Range("a1").Resize(, 3) .Value = Array("From", "Subject", "Received") .Font.Bold = True End With ActiveSheet.Range("a2").Resize(UBound(arrData, 2), UBound(arrData, 1)).Value = WorksheetFunction.Transpose(arrData) ActiveSheet.Range("C2", Range("C2").End(xlDown)).NumberFormat = "m/d/yyyy h:mm" ActiveSheet.Columns.AutoFit End Sub
Hello Forum Guru,
Thank you for providing the code however i have few questions in this regard
First question: As per my earlier post I said i use multiple Mailboxes so how will this Marco would would recognize which Mailbox it has to select
Second question: As mentioned in the above code you have requested to change the subfolder, since poor in coding can you elborate on this comment
Thank you for all your help in this regard
GetDefaultFolder(olFolderInbox) refers to your Inbox.First question: As per my earlier post I said i use multiple Mailboxes so how will this Marco would would recognize which Mailbox it has to select
Second question: As mentioned in the above code you have requested to change the subfolder, since poor in coding can you elborate on this comment
Folders("MySubFolder") refers to a subfolder within your Inbox.
So, for example, if you receive your emails of interest in a subfolder within your Inbox and it's called "Project A", replace "MySubFolder" with "Project A".
Last edited by Domenic; 01-27-2012 at 07:19 AM.
Hello Domenic,
Thank you very much for the explanation and providing the macro.
As mentioned in my first post there are multiple mailboxes mapped to my email.Is there a possiblity that I can use this for this type of mailboxes and also is there a possibility that I can retrive the same information from a sub folder in PST
Thanks for all your support
Let's say that you created a personal folder and named it "2012 Emails", and that you also created a subfolder and called it "Project X". Simply replace...
withSet olFldr = olNS.GetDefaultFolder(olFolderInbox).Folders("MySubFolder")
Set olFldr = olNS.Folders("2012 Emails).Folders("Project X")
Hello Domenic,
Sorry I am getting back to you again on this
In my inbox I have many subfolders, is there a possibility that I can get information for all these sub folders using this macro
Please advise
The following macro will list in the active worksheet the name of each folder within your InBox, along with number of items for each one...
Option Explicit Sub test() Dim olApp As Object Dim olNS As Object Dim olInBox As Object Dim olSubFolder As Object Dim r As Long Set olApp = CreateObject("Outlook.Application") Set olNS = olApp.GetNamespace("MAPI") Set olInBox = olNS.GetDefaultFolder(6) 'olFolderInbox Cells.ClearContents Range("a1").Resize(, 2).Value = Array("Folder", "Items") r = 2 For Each olSubFolder In olInBox.Folders Cells(r, "a").Value = olSubFolder.Name Cells(r, "b").Value = olSubFolder.Items.Count r = r + 1 Next olSubFolder End Sub
Hello Domenic,
I think my question in earlier post might be little confusing because based on the code you provided now it gives the below out
Folder name and no of emails in it.
Actually if you can look into the first macro you provided it give information like from,Subject and Received date and time however it was capturing data from one subfolder of Inbox, my query is, is it possible to capture from,Subject and Received date infromation from all the subfolders, not a single folder.
I am really sorry that I have confused you
Thanks
Do the subfolders in your Inbox also contain subfolders? And, if so, do you want the details for those subfolders as well? Or are you only interested in the folders within your Inbox?
Last edited by Domenic; 02-17-2012 at 11:03 AM.
Yes Domenic few sub folders have sub folders in them, yes I would need the complete data.
Place the following code in a regular module, and run the macro called 'test'...
Option Explicit Dim arrData() As Variant Dim Cnt As Long Sub test() Dim olApp As Object Dim olNS As Object Dim olFldr As Object Set olApp = CreateObject("Outlook.Application") Set olNS = olApp.GetNamespace("MAPI") Set olFldr = olNS.GetDefaultFolder(6) 'olFolderInbox Cnt = 0 Call RecursiveFolders(olFldr) Cells.ClearContents With ActiveSheet.Range("a1").Resize(, 4) .Value = Array("Folder", "From", "Subject", "Received") .Font.Bold = True End With ActiveSheet.Range("A2").Resize(UBound(arrData, 2), UBound(arrData, 1)).Value = WorksheetFunction.Transpose(arrData) ActiveSheet.Range("D2", Range("D2").End(xlDown)).NumberFormat = "m/d/yyyy h:mm" ActiveSheet.Columns.AutoFit End Sub Sub RecursiveFolders(olFolder As Object) Dim olSubFolder As Object Dim olMail As Object For Each olMail In olFolder.Items Cnt = Cnt + 1 ReDim Preserve arrData(1 To 4, 1 To Cnt) arrData(1, Cnt) = olFolder.FolderPath arrData(2, Cnt) = olMail.Sender arrData(3, Cnt) = olMail.Subject arrData(4, Cnt) = olMail.ReceivedTime Next For Each olSubFolder In olFolder.Folders Call RecursiveFolders(olSubFolder) Next olSubFolder End Sub
A small modification in case no mail exists in the specified folder and/or its subfolders. Replace...
withWith ActiveSheet.Range("a1").Resize(, 4) .Value = Array("Folder", "From", "Subject", "Received") .Font.Bold = True End With ActiveSheet.Range("A2").Resize(UBound(arrData, 2), UBound(arrData, 1)).Value = WorksheetFunction.Transpose(arrData) ActiveSheet.Range("D2", Range("D2").End(xlDown)).NumberFormat = "m/d/yyyy h:mm" ActiveSheet.Columns.AutoFit
If Cnt > 0 Then With ActiveSheet.Range("a1").Resize(, 4) .Value = Array("Folder", "From", "Subject", "Received") .Font.Bold = True End With ActiveSheet.Range("A2").Resize(UBound(arrData, 2), UBound(arrData, 1)).Value = WorksheetFunction.Transpose(arrData) ActiveSheet.Range("D2", Range("D2").End(xlDown)).NumberFormat = "m/d/yyyy h:mm" ActiveSheet.Columns.AutoFit Else MsgBox "No mail items are available...", vbExclamation End If
Thank you for providing the code however there is some an error message pops up when I execute the test macro
Run-Time Error:438 "Object Doesn't support this Prpoerty or Method"
Please advise
Sorry, the .Sender property is only available on Excel 2010. Therefore, we'll need to use the .SenderName property instead. Replace...
witharrData(1, Cnt) = olMail.Sender
arrData(1, Cnt) = olMail.SenderName
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks