Get feild details from outlook to excel

THE_NEW_XL_GUY

New Member
Joined
Dec 20, 2017
Messages
47
Hello guys,

can someone help me to get this code work, it just freezes whenever I run macro.. and

in detail:

I have the code below which I got from internet and it is used to get data like subject, received time, sender name etc from specific date which I give as input

later I tweaked it to work with sharedmailbox mails only.

Now problem:

it just freezes and I also want to include categorized mails I mean on what category it is categorized, need that details as well..


Any help, I really appreciate it. thanks in advance!

Code:
Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set olShareName = OutlookNamespace.CreateRecipient("email@exAMPLE.com")
Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox)

i = 1
For Each OutlookMail In Folder.Items
    If OutlookMail.ReceivedTime >= Range("From_date").Value Then
       Range("eMail_subject").Offset(i, 0).Value = OutlookMail.Subject
        Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
        Range("eMail_sender").Offset(i, 0).Value = OutlookMail.SenderName
        Range("eMail_size").Offset(i, 0).Value = OutlookMail.Size
        'Range("eMail_categories").Offset(i, 0).Value = OutlookMail.Categorize // is there way to get categorized mails with name of category 
        
        
        i = i + 1
    End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Writing to cells that way is very inefficient. Instead, use an array to fill it with the desired data, and then transfer the contents of that array to your worksheet. Maybe something like this...

Code:
Sub GetFromOutlook()    Dim OutlookApp As Outlook.Application
    Dim OutlookNamespace As Outlook.Namespace
    Dim olShareName As Outlook.Recipient
    Dim Folder As MAPIFolder
    Dim OutlookMail As Variant
    Dim arrResults() As Variant
    Dim ItemCount As Long
    
    Set OutlookApp = New Outlook.Application
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set olShareName = OutlookNamespace.CreateRecipient("email@exAMPLE.com")
    Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox)
    
    If Folder.Items.Count > 0 Then
        ReDim arrResults(1 To Folder.Items.Count, 1 To 5)
        ItemCount = 0
        For Each OutlookMail In Folder.Items
            ItemCount = ItemCount + 1
            arrResults(ItemCount, 1) = OutlookMail.Subject
            arrResults(ItemCount, 2) = OutlookMail.ReceivedTime
            arrResults(ItemCount, 3) = OutlookMail.SenderName
            arrResults(ItemCount, 4) = OutlookMail.Size
            arrResults(ItemCount, 5) = OutlookMail.Categories
        Next OutlookMail
        Worksheets("Sheet1").Range("A1").Resize(UBound(arrResults, 1), 5) = arrResults
    Else
        MsgBox "No items found!", vbExclamation
    End If
    
    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set olShareName = Nothing
    Set OutlookApp = Nothing
End Sub

Hope this helps!
 
Upvote 0
Hi @Domenic, thanks for replying! so its still freezing when ever I run macro.
and also the macro I posted had option to select a date from which we need the data to be pulled, can you just help me to add from date in code.

I have added it. correct me if wrong

Code:
Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
    Dim OutlookNamespace As Outlook.Namespace
    Dim olShareName As Outlook.Recipient
    Dim Folder As MAPIFolder
    Dim OutlookMail As Variant
    Dim arrResults() As Variant
    Dim ItemCount As Long
    
    Set OutlookApp = New Outlook.Application
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set olShareName = OutlookNamespace.CreateRecipient("example@email[EMAIL="milind.p@.com"].com[/EMAIL]")
    Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox)
    
    If OutlookMail.ReceivedTime >= Range("From_date").Value Then // cell name is given as from_date and run button place next to it run macro.    

If Folder.Items.Count > 0 Then
        ReDim arrResults(1 To Folder.Items.Count, 1 To 5)
        ItemCount = 0
        For Each OutlookMail In Folder.Items
            ItemCount = ItemCount + 1
            arrResults(ItemCount, 1) = OutlookMail.Subject
            arrResults(ItemCount, 2) = OutlookMail.ReceivedTime
            arrResults(ItemCount, 3) = OutlookMail.SenderName
            arrResults(ItemCount, 4) = OutlookMail.Size
            arrResults(ItemCount, 5) = OutlookMail.Categories
        Next OutlookMail
        Worksheets("Sheet1").Range("A5").Resize(UBound(arrResults, 1), 5) = arrResults
    Else
        MsgBox "No items found!", vbExclamation
    End If
    End If
    
    
    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set olShareName = Nothing
    Set OutlookApp = Nothing
End Sub
 
Upvote 0
Try using the Restrict method of the Items object to filter the data based on the date...

Code:
Sub GetFromOutlook()
    Dim OutlookApp As Outlook.Application
    Dim OutlookNamespace As Outlook.Namespace
    Dim olShareName As Outlook.Recipient
    Dim Folder As MAPIFolder
    Dim olItems As Outlook.Items
    Dim OutlookMail As Variant
    Dim arrResults() As Variant
    Dim ItemCount As Long
    
    Set OutlookApp = New Outlook.Application
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set olShareName = OutlookNamespace.CreateRecipient("example@email.com")
    Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox)
    Set olItems = Folder.Items.Restrict("[ReceivedTime] >= '" & Range("From_date").Value & "'")
    
    If olItems.Count > 0 Then
        ReDim arrResults(1 To olItems.Count, 1 To 5)
        ItemCount = 0
        For Each OutlookMail In olItems
            ItemCount = ItemCount + 1
            arrResults(ItemCount, 1) = OutlookMail.Subject
            arrResults(ItemCount, 2) = OutlookMail.ReceivedTime
            arrResults(ItemCount, 3) = OutlookMail.SenderName
            arrResults(ItemCount, 4) = OutlookMail.Size
            arrResults(ItemCount, 5) = OutlookMail.Categories
        Next OutlookMail
        Worksheets("Sheet1").Range("A5").Resize(UBound(arrResults, 1), 5) = arrResults
    Else
        MsgBox "No items found!", vbExclamation
    End If
    
    Set olItems = Nothing
    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set olShareName = Nothing
    Set OutlookApp = Nothing
End Sub

Does this help?
 
Upvote 0
Hi mate, very sorry for delayed reply.

I got the following error while handling: script out of range.

when I debug its showing in line

Code:
Worksheets("Sheet1").Range("A5").Resize(UBound(arrResults, 1), 5) = arrResults
.

so I didn't quite understand what it meant. help me kindly
 
Upvote 0
That line of code transfers the content of the array to a worksheet called "Sheet1" in the active workbook. So if the active workbook doesn't contain a sheet called "Sheet1", you'll get that error. Either make sure that the workbook containing that sheet is the active workbook or qualify the reference to your worksheet...

Code:
Workbooks("Book1.xlsx").[COLOR=#333333]Worksheets("Sheet1").Range("A5").Resize(UBound(arrResults, 1), 5) = arrResults[/COLOR]

Hope this helps!
 
Last edited:
Upvote 0
thanks a ton mate!! it worked. but categorization field is not working. is there way to call inbuilt function just like outlook.subject or outlook.sendername kinda function
 
Upvote 0
outlook.categories should return the category or categories. If it doesn't return anything, the category likely has not been set.
 
Upvote 0
yeah! correct thanks mate. Its working great. One last question does adding another olitems to include "To_date" work?

Does this line code work:

Code:
Set olItems = Folder.Items.Restrict("[ReceivedTime] >= '" & Range("From_date").Value & "'")
    Set olItems2 = Folder.Items.Restrict("[ReceivedTime] >= '" & Range("to_date").Value & "'")

If not any other better option to include to date in code just like from date.


thanks for all the help.
 
Upvote 0
Try...

Code:
Set olItems = Folder.Items.Restrict("[ReceivedTime] >= '" & Range("From_date").Value & "' and [ReceivedTime] <= '" & Range("to_date").Value & "'")

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top