Searching Outlook email (and replying to it) using Excel VBA

ramesses

I want to search ALL my outlook for latest message in a conversation (I use Subject name as search key).

This latest message can be in Inbox, Sent Items, in a sub folder of Inbox, a sub-sub folder of Inbox (anywhere).

I can achieve this by some very tedious code, going through every level of each major folder, but not only this method is very messy, I can't determine if this found message is the latest in this conversation.

I have the following code, which

--> Searches Inbox for "searchKey"

--> If finds it in Inbox folder, replies to it

--> If not, it moves into subfolders of Inbox, and continues the same process

Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olFldr As MAPIFolder
Dim olMail ' As Outlook.MailItem
Dim i As Integer

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set olFldr = Fldr

tryAgain:

    For Each olMail In olFldr.Items
        If InStr(olMail.Subject, searchKey) <> 0 Then
            Set ReplyAll = olMail.ReplyAll
            With ReplyAll
                .HTMLBody = Msg & .HTMLBody
                emailReady = True
                .Display
            End With
        End If
    Next olMail


If Not emailReady Then
    i = i + 1
    If i > Fldr.Folders.Count Then
        MsgBox ("The email with the given subject line was not found!")
        Exit Sub
    Else
        Set olFldr = Fldr.Folders(i)
        GoTo tryAgain
    End If
End If

This code might be confusing and long, so please let me know if you need any clarification.

The question is: How can I search through ALL Outlook, without going manually through every folder/subfolder/sub-subfolder... without this method, and find the LAST message in a specific conversation? Or, at least, how can I optimize this code so I don't miss any folder, and know the dates and times these emails were sent?

Verity

You can use the built in AdvancedSearch function, which returns a Search object containing items. These should have date properties, so you only need your code to go through the search object mailItems and find that with the latest date ( ReceivedTime)?

I would suggest using the bottom example on that page - it gets a table object from the search, and then you use

Set MyTable = MySearch.GetTable  
Do Until MyTable.EndOfTable  
    Set nextRow = MyTable.GetNextRow()  
    Debug.Print nextRow("ReceivedTime")  
Loop

From there, you can do the comparison to find the latest time, and if you want to do something with the mailitem you would need to obtain the "EntryID" column from the table. Then use the GetItemFromID method of the NameSpace object to obtain a full item, since the table returns readonly objects.

You can also apply a date filter to the search if you wish, if you knew a minimum date for instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Excel VBA for searching in mails of Outlook

From Dev

Excel VBA: Sent Outlook email does not include pasted Range

From Dev

Searching an excel with two search words (multiple lookup) using vba macro

From Dev

Inserting Signature into Outlook email from Excel VBA

From Dev

How to email worksheet to yourself in Excel/VBA (change the email dynamically depending on outlook account open)

From Dev

Outlook VBA Email Autosave

From Dev

VBA Using Outlook to open excel not opening

From Dev

Adding multiple attachments to a single email using outlook VBA

From Dev

Excel VBA array of strings to Outlook email "To" field

From Dev

Replying to an email with PHPmailer

From Dev

Using VBA to email a file without using outlook

From Dev

Using VBA to attach a file in an outlook email

From Dev

Integrate Outlook "Run as Script" rule into Excel VBA code that sends email

From Dev

Copying a rich text table from an Outlook email to Excel using VBA?

From Dev

Inserting Excel file as an image in email using Outlook 2010

From Dev

Exporting Received Email daily Tally from Outlook to File using Excel VBA

From Dev

Using Excel VBA to create email in Outlook 2010 from template

From Dev

Excel VBA: Move Outlook email in public folder without using GetNamespace("MAPI")

From Dev

How to filter Outlook email using a Rule with a VBA script?

From Dev

Setting a background image using HTML in an outlook email using Excel VBA

From Dev

Chinese letters showing up when replying to email in Outlook

From Dev

Outlook 2016 with multiple account setup using the wrong "From" email when replying or forwarding messages

From Dev

VBA - How to store .SentOn in excel before sending an email in Outlook

From Dev

Microsoft Bot Framework: Bot not replying to email when using Exchange Email

From Dev

Append senders email to the front of the attachment in Outlook using VBA

From Dev

Issue passing date of flagged outlook email messages using vba

From Dev

Extracting a word from Outlook Email Body while replying email with predefined Template

From Dev

Excel to automate email in Outlook using specific fields in the excel sheet

From Dev

Sending Email with PNG image in Outlook Body using VBA

Related Related

  1. 1

    Excel VBA for searching in mails of Outlook

  2. 2

    Excel VBA: Sent Outlook email does not include pasted Range

  3. 3

    Searching an excel with two search words (multiple lookup) using vba macro

  4. 4

    Inserting Signature into Outlook email from Excel VBA

  5. 5

    How to email worksheet to yourself in Excel/VBA (change the email dynamically depending on outlook account open)

  6. 6

    Outlook VBA Email Autosave

  7. 7

    VBA Using Outlook to open excel not opening

  8. 8

    Adding multiple attachments to a single email using outlook VBA

  9. 9

    Excel VBA array of strings to Outlook email "To" field

  10. 10

    Replying to an email with PHPmailer

  11. 11

    Using VBA to email a file without using outlook

  12. 12

    Using VBA to attach a file in an outlook email

  13. 13

    Integrate Outlook "Run as Script" rule into Excel VBA code that sends email

  14. 14

    Copying a rich text table from an Outlook email to Excel using VBA?

  15. 15

    Inserting Excel file as an image in email using Outlook 2010

  16. 16

    Exporting Received Email daily Tally from Outlook to File using Excel VBA

  17. 17

    Using Excel VBA to create email in Outlook 2010 from template

  18. 18

    Excel VBA: Move Outlook email in public folder without using GetNamespace("MAPI")

  19. 19

    How to filter Outlook email using a Rule with a VBA script?

  20. 20

    Setting a background image using HTML in an outlook email using Excel VBA

  21. 21

    Chinese letters showing up when replying to email in Outlook

  22. 22

    Outlook 2016 with multiple account setup using the wrong "From" email when replying or forwarding messages

  23. 23

    VBA - How to store .SentOn in excel before sending an email in Outlook

  24. 24

    Microsoft Bot Framework: Bot not replying to email when using Exchange Email

  25. 25

    Append senders email to the front of the attachment in Outlook using VBA

  26. 26

    Issue passing date of flagged outlook email messages using vba

  27. 27

    Extracting a word from Outlook Email Body while replying email with predefined Template

  28. 28

    Excel to automate email in Outlook using specific fields in the excel sheet

  29. 29

    Sending Email with PNG image in Outlook Body using VBA

HotTag

Archive