Help with identifying error with VBA - Search outlook and return results to Excel.

owen4512

Board Regular
Joined
Dec 10, 2014
Messages
71
Hi all,

I'm currently in the process of creating something that will search my outlook inbox (Email body and subject) for a 'Key word' and list results in excel. I have created the below and completed some testing however i'm not confident in the accuracy of the results. I had to stop the macro mid running as i was expecting around 30 results whereas the results exceeded 3,000.

I'd looking for help in identifying any errors in the below VBA :) Any help is much appreciated!

VBA Code:
Option Explicit

Public Sub Search_Outlook_Emails()

Dim outApp As Outlook.Application
Dim outNs As Outlook.Namespace
Dim outStartFolder As Outlook.MAPIFolder
Dim foundEmail As Outlook.MailItem

Set outApp = New Outlook.Application
Set outNs = outApp.GetNamespace("MAPI")

Set outStartFolder = outNs.Folders("test@outlook.co.uk").Folders("Inbox")

If Not outStartFolder Is Nothing Then

    Set foundEmail = Find_Email_In_Folder(outStartFolder, Sheet1.Range("E2").Text)
    

End If
End Sub

Private Function Find_Email_In_Folder(outFolder As Outlook.MAPIFolder, findText As String) As Outlook.MailItem

Dim outItem As Object
Dim outMail As Outlook.MailItem
Dim outSubFolder As Outlook.MAPIFolder
Dim i As Long
Dim arrHeader As Variant

 Debug.Print outFolder.FolderPath

 Set Find_Email_In_Folder = Nothing

    i = 1
    While i <= outFolder.Items.Count And Find_Email_In_Folder Is Nothing
    
    Set outItem = outFolder.Items(i)
    
    If outItem.Class = Outlook.OlObjectClass.olMail Then
    
    Set outMail = outItem
    If InStr(1, outMail.Body, findText, vbTextCompare) > 0 Then Set Find_Email_In_Folder = outMail
    
        Sheet1.Cells(i + 1, "A").Value = outItem.CreationTime
        Sheet1.Cells(i + 1, "B").Value = outItem.Subject
        
    End If
    i = i + 1

    Wend

DoEvents

    i = 1
    While i <= outFolder.Folders.Count And Find_Email_In_Folder Is Nothing
        
    Set outSubFolder = outFolder.Folders(i)
    'Only check mail item folders
    
    If outSubFolder.DefaultItemType = Outlook.olMailItem Then Set Find_Email_In_Folder = Find_Email_In_Folder(outSubFolder, findText)
    
    i = i + 1

Wend
End Function
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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