I have put together some code in excel from various sources online(VBA X and others) to extract outlook emails from a series of public folders, its working fine but I would like to amend the code to only extract email based on a date range. At the moment its taking ages to run as its extracting all emails everytime I run it, I only need to run a weeks worth each time.
I will update the week commencing date(start date) on a summary sheet in excel and would like the macro to extract only emails in the folder based on that week(7days).
I'm not sure if I can do that using the method currently used.
Any help with this matter would be greatly appreciated!
Current working code below that is used for 1 of the folder counts.
Dim objOutlook As Object, objnSpace As Object
Dim objfolder As Object
Dim EmailItemCount As Integer, I As Integer, EmailCount As Integer
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")
Set objfolder = objnSpace.Folders("Mailbox - #Shared Mailbox - PPNB").Folders("Inbox")
On Error Resume Next
If Err.Number <> 0 Then
MsgBox "No such folder named Inbox.", 48, "Cannot continue"
Application.ScreenUpdating = False
Workbooks("NB Weekly Email Report").Sheets("Summary").Activate
Range("B18").Value = Now()
Workbooks("NB Weekly Email Report").Sheets("Pending").Activate
EmailItemCount = objfolder.Items.Count
I = 0: EmailCount = 0
While I < EmailItemCount
I = I + 1
If I Mod 50 = 0 Then Application.StatusBar = "Reading e-mail messages " & _
Format(I / EmailItemCount, "0%") & "..."
EmailCount = EmailCount + 1
Cells(EmailCount + 1, 1).Formula = .Subject
Cells(EmailCount + 1, 2).Formula = Format(.ReceivedTime, "dd.mm.yyyy hh:mm:ss")
Cells(EmailCount + 1, 3).Formula = .Attachments.Count
Cells(EmailCount + 1, 4).Formula = Not .UnRead
Cells(EmailCount + 1, 5).Formula = .SenderName
Cells(EmailCount + 1, 6).Formula = .ReceivedTime
Cells(EmailCount + 1, 7).Formula = .LastModificationTime
Cells(EmailCount + 1, 8).Formula = .ReplyRecipientNames
Cells(EmailCount + 1, 9).Formula = .SentOn
Cells(EmailCount + 1, 10).Value = objfolder.Name
Application.Calculation = xlCalculationAutomatic
Set objfolder = Nothing
Formatting tags added by mark007