Search for email in all outlook subfolders

yarmulke

New Member
Joined
Jul 22, 2020
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Public Sub SearchInOutlook()
Dim CurCell As Range
For Each CurCell In Selection
Call OutlookLaunch(CurCell.Value)
Next

End Sub

Private Sub OutlookLaunch(SearchString As String)
Dim outlookApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olMail As Variant
Dim dtToday As Date
dtToday = Date

'Call GetEmailFromNonDefaultInbox
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim strFilter As String



Set myAccounts = myOlApp.GetNamespace("MAPI").Stores
For i = 1 To myAccounts.Count
If myAccounts.Item(i).DisplayName = "myspecificemailbox@gmail.com" Then

Set myInbox = myAccounts.Item(i).GetDefaultFolder(olFolderInbox)

Exit For
End If
Next


Set myitems = myInbox.Items


For Each olMail In myitems

If (InStr(1, olMail.Subject, SearchString, vbTextCompare) > 0) Then
olMail.Display
Exit For
End If

Next


End Sub





So far this script will find the email i selected in excel but it will only find it if it is in the main email folder "Inbox".

I need this script to look through every subfolder in myspecificemailbox@gmail.com to find my emails.

using any default inbox scripts will not work as this is not my default inbox.

any help is appreciated thank you.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Please use VBA code tags - the VBA icon in the message editor bar.

You need a recursive procedure which loops through all the emails in a folder and calls itself for each subfolder, like this:
VBA Code:
Private Sub Search_Outlook_Folder(ByVal outFolder As Outlook.MAPIFolder, SearchSubject As String)
    
    Dim outItem As Object
    Dim outMailItem As Outlook.MailItem
    Dim outSubfolder As Outlook.MAPIFolder
    
    For Each outItem In outFolder.Items
        If outItem.Class = olMail Then
            Set outMailItem = outItem
            If InStr(1, outMailItem.Subject, SearchSubject, vbTextCompare) > 0 Then
                outMailItem.Display
            End If
        End If
    Next
    
    'Recurse through subfolders of this Outlook folder
    
    For Each outSubfolder In outFolder.Folders
        Search_Outlook_Folder outSubfolder, SearchSubject
    Next
    
End Sub
To start the search from your Inbox folder and its subfolders, call the procedure like this:
VBA Code:
    Search_Outlook_Folder myInbox, SearchString
To start the search from your Account/Store and its subfolders, call the procedure like this:
VBA Code:
    Search_Outlook_Folder myInbox.Parent, SearchString
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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