VBA can't read all email from outlook

123456789546 1 Reputation point
2022-08-07T05:35:59.873+00:00

Hi, I am facing a difficult question. I am writing a macro to reply a specific email. However, I can't exact all mail in the outlook. Here is the code

Sub ReplyMail()

Dim olApp As Outlook.Application

Dim olNs As Namespace

Dim Fldr As MAPIFolder

Dim olMail As Variant

Dim SigString As String

Dim Signature As String

Dim i As Integer

Dim subj As String

Set olApp = New Outlook.Application

Set olNs = olApp.GetNamespace("MAPI")

Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("ABCDE")

i = 1

For Each olMail In Fldr.Items

subj = olMail.Subject

If InStr(olMail.Subject, "Exposure Statement - COB date") <> 0 Then 'where date is a date that changes every wednesday

With olMail.Reply

    .to = "******@domain.com;******@domain.com"   

    .CC = "******@domain.com;******@domain.com"   

    .Body = "Dear All," & Chr(10) & _   

    Chr(10) & "we agree with your portfolio here attached and according to it we see no move for today." & _   

    Chr(10) & "        Best Regards." & _   

    Chr(10) & _   

    Chr(10) & Signature   

    .Display   

End With   

i = i + 1

End If

Next olMail

End Sub

The code was copy from the other post. Although this code can read so email, the extracted email was several days ago. It would be much appreciated if the code can read the newest email from outlook.

Outlook | Windows | Classic Outlook for Windows | For business
Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Oskar Shon 866 Reputation points
    2022-11-08T21:18:40.53+00:00

    Hi
    you using that for what?
    subj = olMail.Subject

    that variable olMail should be an object or mailitem if you use references or into Outlook code.

    if your loop do not works fine then try to use backsword loop
    from x = Fldr.Items.count to 1 step -1
    and Fldr.items(x) 'to check

    Anyway
    try to using rules with code/script to check data with your date inside Subject?
    your code 'll simplest, without loop

    Regards

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.