• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to search a string in e-mail through Excel VBA

Status
Not open for further replies.
Hi @Deepak Sir, hope you are doing good.

I am facing an issue, the code I am using checks each mail in Inbox, 'For Each Mail in Inbox'.

There are more than 20,000 e-mails in Inbox, though there are only 50-60 e-mails for =today but since macro checks each e-mail from Inbox, the code gets hanged.

Is there anyway to skip 'For Each' loop, my entire logic is based on 'For Each'.
Could you please suggest alternative to this. Please help if you get time.

Have a nice weekend (in advance). :)
 
Hi @Deepak Sir, hope you are doing good.
I have changed my loop from 'For Each' to 'For i = 1 to predefined number'.
It seems to be working much faster.
This way I am able to limit e-mails that needs to be searched.

Please find below code for your reference.

Code:
For i = 1 To MacroBook.Worksheets("Macro").Range("A1").Value
        Set myitem = myFolder.Items(i)
        Set olProperty = myitem.UserProperties.Add("KeyWord", olText)
        msgtext = myitem.Body
        Application.StatusBar = "Rule 1 " & myFolder.Items(i).Subject & " " & i
        If olProperty.Value = "" Then
            If (InStr(1, myFolder.Items(i).Body, KWrd1, vbTextCompare) > 0) Then
                If VBA.DateValue(VBA.Now) - VBA.DateValue(myFolder.Items(i).ReceivedTime) = 0 Then
                    ExportSht.Columns("A:BA").Clear
                    ExportSht.Range("A1").Value = myFolder.Items(i).Body
                    MacroBook.Activate
                    ExportSht.Select
                   
                    EmailTime = myFolder.Items(i).ReceivedTime
                    EmailAddress = myFolder.Items(i).SenderEmailAddress
                    EmailSubject = myFolder.Items(i).Subject
                    EmailCategory = myFolder.Items(i).Categories
                   
                    Call Export_Email
                    Call Prepare_Template
                End If
            End If
        End If
    Next

Have a nice week ahead. :)
 
Status
Not open for further replies.
Back
Top