+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 31

Thread: Capture in time and out time of email sent via outlook

  1. #1
    Registered User
    Join Date
    06-03-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    50

    Unhappy Capture in time and out time of email sent via outlook

    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

  2. #2
    Forum Guru
    Join Date
    06-18-2004
    Location
    Canada
    Posts
    1,330

    Re: Capture in time and out time of email sent via outlook

    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
    
    Domenic
    Microsoft MVP - Excel
    xl-central.com, "Your Quick Reference to Excel Solutions"

  3. #3
    Registered User
    Join Date
    06-03-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    50

    Re: Capture in time and out time of email sent via outlook

    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

  4. #4
    Forum Guru
    Join Date
    06-18-2004
    Location
    Canada
    Posts
    1,330

    Re: Capture in time and out time of email sent via outlook

    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
    GetDefaultFolder(olFolderInbox) refers to your Inbox.

    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.
    Domenic
    Microsoft MVP - Excel
    xl-central.com, "Your Quick Reference to Excel Solutions"

  5. #5
    Registered User
    Join Date
    06-03-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    50

    Re: Capture in time and out time of email sent via outlook

    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

  6. #6
    Forum Guru
    Join Date
    06-18-2004
    Location
    Canada
    Posts
    1,330

    Re: Capture in time and out time of email sent via outlook

    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...

    Set olFldr = olNS.GetDefaultFolder(olFolderInbox).Folders("MySubFolder")
    with

    Set olFldr = olNS.Folders("2012 Emails).Folders("Project X")
    Domenic
    Microsoft MVP - Excel
    xl-central.com, "Your Quick Reference to Excel Solutions"

  7. #7
    Registered User
    Join Date
    06-03-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    50

    Re: Capture in time and out time of email sent via outlook

    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

  8. #8
    Forum Guru
    Join Date
    06-18-2004
    Location
    Canada
    Posts
    1,330

    Re: Capture in time and out time of email sent via outlook

    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
    
    Domenic
    Microsoft MVP - Excel
    xl-central.com, "Your Quick Reference to Excel Solutions"

  9. #9
    Registered User
    Join Date
    06-03-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    50

    Re: Capture in time and out time of email sent via outlook

    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

  10. #10
    Forum Guru
    Join Date
    06-18-2004
    Location
    Canada
    Posts
    1,330

    Re: Capture in time and out time of email sent via outlook

    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.
    Domenic
    Microsoft MVP - Excel
    xl-central.com, "Your Quick Reference to Excel Solutions"

  11. #11
    Registered User
    Join Date
    06-03-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    50

    Re: Capture in time and out time of email sent via outlook

    Yes Domenic few sub folders have sub folders in them, yes I would need the complete data.

  12. #12
    Forum Guru
    Join Date
    06-18-2004
    Location
    Canada
    Posts
    1,330

    Re: Capture in time and out time of email sent via outlook

    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
        
    
    Domenic
    Microsoft MVP - Excel
    xl-central.com, "Your Quick Reference to Excel Solutions"

  13. #13
    Forum Guru
    Join Date
    06-18-2004
    Location
    Canada
    Posts
    1,330

    Re: Capture in time and out time of email sent via outlook

    A small modification in case no mail exists in the specified folder and/or its subfolders. Replace...

        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
    with

        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
    Domenic
    Microsoft MVP - Excel
    xl-central.com, "Your Quick Reference to Excel Solutions"

  14. #14
    Registered User
    Join Date
    06-03-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    50

    Re: Capture in time and out time of email sent via outlook

    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

  15. #15
    Forum Guru
    Join Date
    06-18-2004
    Location
    Canada
    Posts
    1,330

    Re: Capture in time and out time of email sent via outlook

    Sorry, the .Sender property is only available on Excel 2010. Therefore, we'll need to use the .SenderName property instead. Replace...

    arrData(1, Cnt) = olMail.Sender
    with

    arrData(1, Cnt) = olMail.SenderName
    Domenic
    Microsoft MVP - Excel
    xl-central.com, "Your Quick Reference to Excel Solutions"

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0