I changed the code to this, then the problem solved. But since I Put the criteria for the starting date there is a gap until it reaches the critieria in excel it shows empty rows
Option Explicit
Sub Getinboxcontents()
Dim ol As Outlook.Application
Dim ns As Outlook.Namespace
Dim fol As Outlook.Folder
Dim I As Object
Dim mi As Outlook.MailItem
Dim n As Long
n = 2
Dim rh As Double
rh = Range("A1").RowHeight
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set fol = ns.GetDefaultFolder(olFolderInbox)
For Each I In fol.items
If I.Class = olMail Then
n = n + 1
Set mi = I
If mi.ReceivedTime >= Range("B1").Value And mi.ReceivedTime <= Range("C1").Value Then
Cells(n, 1).Value = mi.SenderName
Cells(n, 2).Value = mi.Subject
Cells(n, 3).Value = mi.ReceivedTime
Cells(n, 4).Value = mi.Body
End If
End If
Next I
Range("A1").CurrentRegion.EntireColumn.AutoFit
Range("A1").CurrentRegion.EntireRow.RowHeight = rh
Set fol = Nothing
Set ns = Nothing
Set ol = Nothing
End Sub