Hi. I have found how to export data from a shared mailbox in outlook from a previous post using the below code. This works fine but I want to be able to extract the data between two specified dates that might change (between 04 Feb and 13 Feb for example). Is there a way to do this rather than just bringing back today’s date as works with this code? Thank you very much.
Option Explicit
Public Sub ReadOutlookEmails()
Dim objFolder As Outlook.Folder
Dim objNamespace As Outlook.Namespace
Dim objMail As Outlook.MailItem
Dim lngCounter As Long
Set objNamespace = Outlook.GetNamespace("MAPI")
Set objFolder = objNamespace.PickFolder
If TypeName(objFolder) = "Nothing" Then
Exit Sub
End If
Dim varItems As Variant
Set varItems = objFolder.Items.Restrict("[Received] >= '" & Format(Split(Now(), " ")(0) & " 12:00am", "ddddd h:nn AMPM") & "'") ' Today
For lngCounter = 1 To varItems.Count
Set objMail = varItems(lngCounter)
shMailReport.Range("A" & lngCounter + 1).Value = objMail.SenderName
shMailReport.Range("B" & lngCounter + 1).Value = objMail.Subject
shMailReport.Range("C" & lngCounter + 1).Value = objMail.Size
shMailReport.Range("D" & lngCounter + 1).Value = objMail.ReceivedTime
shMailReport.Range("E" & lngCounter + 1).Value = objMail.Categories
Next
MsgBox "Finished", vbInformation
End Sub
Option Explicit
Public Sub ReadOutlookEmails()
Dim objFolder As Outlook.Folder
Dim objNamespace As Outlook.Namespace
Dim objMail As Outlook.MailItem
Dim lngCounter As Long
Set objNamespace = Outlook.GetNamespace("MAPI")
Set objFolder = objNamespace.PickFolder
If TypeName(objFolder) = "Nothing" Then
Exit Sub
End If
Dim varItems As Variant
Set varItems = objFolder.Items.Restrict("[Received] >= '" & Format(Split(Now(), " ")(0) & " 12:00am", "ddddd h:nn AMPM") & "'") ' Today
For lngCounter = 1 To varItems.Count
Set objMail = varItems(lngCounter)
shMailReport.Range("A" & lngCounter + 1).Value = objMail.SenderName
shMailReport.Range("B" & lngCounter + 1).Value = objMail.Subject
shMailReport.Range("C" & lngCounter + 1).Value = objMail.Size
shMailReport.Range("D" & lngCounter + 1).Value = objMail.ReceivedTime
shMailReport.Range("E" & lngCounter + 1).Value = objMail.Categories
Next
MsgBox "Finished", vbInformation
End Sub