简体   繁体   中英

Excel VBA with Outlook: How to use AdvancedSearch method to search for specific emails in Outlook?

I have a program here that searches thru each emails in Sent Items folder of Outlook until it finds the tag(URL) in the specified email, opens it and sends a reply.

But I have a lot of emails inside the Sent Items folder, so it's taking so much time for searching thru each emails.

I've read that AdvancedSearch method is a faster way for searching specific emails. But I don't really know how it works that's why I need your help guys.

How do I use AdvancedSearch method with this type of program?

Appreciate your help guys.

Set olApp = New Outlook.Application
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderSentMail)

For Each olMail In olFolder.Items

    tagLink = Sheets("Data").Cells(rowCount, 6)

    If InStr(olMail.HTMLBody, tagLink) <> 0 Then

    With olMail.Reply

        .Display
        .To = sMailTo
        .CC = sMailCC
        .HTMLBody = mailBody & vbLf & .HTMLBody

        .Send

    End With

    End If

Next olMail

For Each olMail In olFolder.Items

Don't iterate over all items in the folder. Especially if you have got a huge amount of items in the folder.

Instead, you may consider using the Restrict or Find/FindNext methods of the Items class. You may find them described in depth in the following articles:

And of course you may consider using the AdvancedSearch method of the Application class. The main benefit of using this method is the ability to get search running in the background and search for items in deferent/multiple folders. Read more about all the benefits in the Advanced search in Outlook programmatically: C#, VB.NET article.

Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM