atuljadhavnetafim
Active Member
- Joined
- Apr 7, 2012
- Messages
- 341
- Office Version
- 365
- Platform
- Windows
Hi
i am using this code to get email subject line which received today in outlook, but it give me only 12 subject line in excel while it is more than 100 emails in today's date.
there is no restriction set in code but still not getting result more than 12.
can you please help
i am using this code to get email subject line which received today in outlook, but it give me only 12 subject line in excel while it is more than 100 emails in today's date.
there is no restriction set in code but still not getting result more than 12.
can you please help
VBA Code:
Sub GetSubjectLineFromOutlook()
Dim olApp As Object ' Outlook.Application
Dim olNamespace As Object ' Outlook.Namespace
Dim olFolder As Object ' Outlook.MAPIFolder
Dim olItems As Object ' Outlook.Items
Dim olMail As Object ' Outlook.MailItem
Dim today As Date
Dim subject As String
Dim wb As Workbook
Dim ws As Worksheet
Dim row As Long
Dim batchSize As Integer
Dim startIndex As Integer
' Set the workbook and worksheet explicitly
Set wb = ThisWorkbook
Set ws = wb.Worksheets("YourSheetName") ' Replace "YourSheetName" with the actual sheet name
' Create Outlook application object
Set olApp = CreateObject("Outlook.Application")
' Get the MAPI namespace
Set olNamespace = olApp.GetNamespace("MAPI")
' Get the Inbox folder
Set olFolder = olNamespace.GetDefaultFolder(6) ' 6 represents the Inbox folder
' Get today's date
today = Date
' Start from the first row in the worksheet
row = 1
' Set the batch size and initial start index
batchSize = 100 ' Adjust batch size as needed
startIndex = 1
' Loop through items in smaller batches until all items are processed
Do
' Get the items in the Inbox folder for the current batch
Set olItems = olFolder.Items
' Sort the items by ReceivedTime in ascending order
olItems.Sort "ReceivedTime", False
' Reset the olMail object
Set olMail = Nothing
' Loop through each email in the current batch
For Each olMail In olItems
' Check if the item is a MailItem
If olMail.Class = 43 Then ' 43 represents a MailItem
' Check if the email was received today
If DateValue(olMail.ReceivedTime) = today Then
' Get the subject line of the email
subject = olMail.subject
' Store the subject line in the worksheet
ws.Cells(row, 1).Value = subject
' Move to the next row
row = row + 1
End If
End If
' Exit the loop if the desired batch size is reached
If row > startIndex + batchSize - 1 Then Exit For
Next olMail
' Update the start index for the next batch
startIndex = row
' Clean up objects
Set olItems = Nothing
Loop While Not olMail Is Nothing
' Clean up objects
Set olFolder = Nothing
Set olNamespace = Nothing
Set olApp = Nothing
End Sub