The following code is working, but I have a few questions.
I have more than one email address in Outlook. I would like to specify the email address in Excel. Let's say in "Control" tab, cell "b1" is an email address. How can I specify it?
I want to specify a non-default folder name (not Inbox, Sent, Drafts or Delete) by name in Excel. Let’s say in the "Control" tab, cell "c1" is a folder name. How can I specify it?
Sub GetFromInbox()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim i, ij As Integer
Dim tt As Date
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
i = 1
ij = 0
x = Date
' Now. the following "For each next " code starts to look in the oldest email!
' So how can I change the code if the code starts from the newest?
For Each olMail In Fldr.Items
ij = ij + 1
'If IsNumeric((Format(olMail.ReceivedTime, "dd/mm/yy"))) Then
Sheets("test").Range("a1").Select
Sheets("test").Range("I1").Clear
Sheets("test").Range("I2") = ij
Sheets("test").Range("I1").Value = (Format(olMail.ReceivedTime, "dd/mm/yy"))
Sheets("test").Range("I1").NumberFormat = "dd/mm/yy"
tt = Sheets("test").Range("I1")
' MsgBox ("Y-tt=" & tt & " receivedtime=" & olMail.ReceivedTime)
'Else
'tt = 0
'MsgBox ("N-tt=" & tt & " receivedtime=" & olMail.ReceivedTime)
'End If
' tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
If tt >= Range("H1") Then
'If InStr(olMail.Subject, "others") > 0 And tt >= Range("h1") Then
If InStr(olMail.Subject, "others") > 0 Then
ActiveSheet.Range("h2") = "y"
ActiveSheet.Cells(i, 1).Value = olMail.Subject
ActiveSheet.Cells(i, 2).Value = olMail.ReceivedTime
ActiveSheet.Cells(i, 3).Value = olMail.SenderName
tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
ActiveSheet.Cells(i, 4).Value = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
' tt = ActiveSheet.Cells(i, 4).Value
ActiveSheet.Cells(i, 5).Value = (Format(olMail.ReceivedTime, "hh:mm"))
MsgBox ("tt=" & tt)
i = i + 1
End If
Else
Sheets("test").Range("h2") = "N"
End If
Next olMail
Set Fldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
'tt = ""
End Sub