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

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

From Dev

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

From Dev

Using Excel VBA to create email in Outlook 2010 from template

From Dev

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

From Dev

Inserting Signature into Outlook email from Excel VBA

From Dev

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

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

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

From Dev

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

From Dev

Chinese letters showing up when replying to email in Outlook

From Dev

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

From Dev

Excel VBA: Sent Outlook email does not include pasted Range

From Dev

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

From Dev

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

From Dev

Outlook VBA Email Autosave

From Dev

VBA Using Outlook to open excel not opening

From Dev

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

From Dev

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

From Dev

Adding multiple attachments to a single email using outlook VBA

From Dev

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

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

Sending Email with PNG image in Outlook Body using VBA

From Dev

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

From Dev

Replying to an email with PHPmailer

From Dev

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

From Dev

Inserting Excel file as an image in email using Outlook 2010

Related Related

  1. 1

    Excel VBA for searching in mails of Outlook

  2. 2

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

  3. 3

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

  4. 4

    Using Excel VBA to create email in Outlook 2010 from template

  5. 5

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

  6. 6

    Inserting Signature into Outlook email from Excel VBA

  7. 7

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

  8. 8

    Using VBA to email a file without using outlook

  9. 9

    Using VBA to attach a file in an outlook email

  10. 10

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

  11. 11

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

  12. 12

    Chinese letters showing up when replying to email in Outlook

  13. 13

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

  14. 14

    Excel VBA: Sent Outlook email does not include pasted Range

  15. 15

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

  16. 16

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

  17. 17

    Outlook VBA Email Autosave

  18. 18

    VBA Using Outlook to open excel not opening

  19. 19

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

  20. 20

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

  21. 21

    Adding multiple attachments to a single email using outlook VBA

  22. 22

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

  23. 23

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

  24. 24

    Issue passing date of flagged outlook email messages using vba

  25. 25

    Sending Email with PNG image in Outlook Body using VBA

  26. 26

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

  27. 27

    Replying to an email with PHPmailer

  28. 28

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

  29. 29

    Inserting Excel file as an image in email using Outlook 2010

HotTag

Archive