Excel Vba loop through multiple outlook emails to find subject match?

yousufj56

Board Regular
Joined
May 22, 2014
Messages
51
Hi,

Currently, I'm extracting tables from a specified Outlook email address. But now i want to make this VBA look through 2 different Outlook email addresses in order to find the specified email subject.

Can someone help me figure out how this loop would work please?

Below is the code I currently have. Fyi, I only included the relevant code. If you need the full code, please let me know. I'm pretty sure that the loop will have to take place on the line: "Set oMapi..."


Code:
' point to first desired email address
Const strMail As String = "first.email@emailaddress.com"


' point to first desired email address
Const strMail2 As String = "second.email@emailaddress.com"


Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox")


For Each oItem In oMapi.Items
    If oItem.Subject = myvar Then
    
            Exit For
    End If
Next oItem


If Not oItem Is Nothing Then


' get html table from email object
Dim HTMLdoc As MSHTML.HTMLDocument
Dim tables As MSHTML.IHTMLElementCollection
Dim table As MSHTML.HTMLTable

Thanks in advance
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this. You need to DIM your o*-variables though

Code:
Option Explicit 'Always use Option Explicit to avoid hard-to-find variable naming errors
'*
'* The email adddesses in question
'* Add as many as you like separated by semicolon
'*
Const strMail As String = "first.email@emailaddress.com;second.email@emailaddress.com"
'********************************
'* Test the code from here
'*
Sub testCode()
    Call findSubject("the subject I want to find")
End Sub
'**************************************************
'* Find an email with a specific subject line
'*
Sub findSubject(theSubject As String)
    Dim addressCount As Integer
    Dim i As Integer
    Dim found As Boolean
    Dim emailAddresses() As String               'String array


    emailAddresses = Split(strMail, ";")         'Fill the array with our email addresses
    addressCount = UBound(emailAddresses)        'Number of email addresses - 1


    For i = 0 To addressCount                    '0-indexed
        Set oMapi = oApp.GetNamespace("MAPI").Folders(emailAddresses(i)).Folders("inbox") 'Get one email address to work with
        found = False                            'Not found yet
        For Each oItem In oMapi.Items
            If oItem.Subject = theSubject Then  'We have found our email with the specific subject
                found = True
                Exit For
            End If
        Next oItem
        If found = True Then Exit For 'Exit outer loop as well
    Next i
    If found Then 'Handle found subject here
    ' ...
    End If
End Sub
 
Upvote 0
Sorry for the late reply. It looks like your code will work, but i'm having issues with the fact that one of the email addresses is actually a shared folder.

I think i have to use "GetSharedDefaultFolder" along with "CreateRecipient" but not sure how to mesh those together.

Any ideas?
 
Upvote 0

Forum statistics

Threads
1,214,553
Messages
6,120,184
Members
448,949
Latest member
keycalinc

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top