Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to read email from outlook in excel macro i want to add some filter for fetching email like subject from date etc.

What I have tried:

I tried condition but not working below is the code.
Public Sub CheckEmail_BlueRecruit()

    ' Declare objects
    Dim outlookApp As Outlook.Application
    Dim outlookNamespace As Outlook.Namespace
    Dim outlookFolder As Outlook.MAPIFolder

    ' Declare other variables
    Dim filterKeywords As String
    Dim filter As String

    ' Init objects
    Set outlookApp = New Outlook.Application
    Set outlookNamespace = Outlook.GetNamespace("MAPI")
    Set outlookFolder = outlookNamespace.GetDefaultFolder(olFolderInbox)

    ' Init other variables
    filterKeywords = "financial"
    filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " LIKE '%" & kakakaka & " %'"
    Debug.Print filter

    ' Loop through folders
    LoopFolders outlookFolder, filter


End Sub
Posted
Updated 15-Apr-24 5:40am
v2
Comments
Richard Deeming 15-Apr-24 5:56am    
Aside from the fact that your filter construction uses a variable which isn't declared, and the fact that you haven't shown the LoopFolders function which (presumably) actually uses the filter, you also haven't explained what "not working" means.

We cannot see your screen, access your computer, or read your mind. The only information we have to work with is what you provide in your question. If you can't explain your problem clearly, then nobody will be able to help you.

1 solution

As Richard mentioned above, you have no code showing how you are going to loop through the mail items. You also need the 'Outlook.Items' and 'Outlook.MailItem' objetcs for it to work properly.

Normally I will not show pointers with as little effort shown as in yours, I do however have something similar written a while back and this should point you on the right direction, use the sub and then link it to your sheet -

VB
Sub ReadEmailsFromOutlook()
    Dim olApp As Outlook.Application
    Dim olNs As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder
    Dim olItems As Outlook.Items
    Dim olMail As Outlook.MailItem
    Dim i As Long
    Dim lastRow As Long

    Set olApp = New Outlook.Application    
    Set olNs = olApp.GetNamespace("MAPI")
    'Access your Inbox folder...
    Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
    
    'Filter emails based on your criteria...
    Set olItems = olFolder.Items.Restrict("[Subject] like '%keyword%' And [SentOn] >= #4/1/2024# And [SenderName] = 'sender@example.com'")
    
    'Get the last row in your worksheet to add the data to...
    lastRow = Sheets("Sheet1").UsedRange.Rows.Count + 1
    
    'Loop through filtered emails as per your search criteria above...
    For i = 1 To olItems.Count
        Set olMail = olItems.Item(i)
        
        'Add the email details to your Excel workbook/sheet...
        Sheets("Sheet1").Cells(lastRow, 1).Value = olMail.Subject
        Sheets("Sheet1").Cells(lastRow, 2).Value = olMail.SenderName
        Sheets("Sheet1").Cells(lastRow, 3).Value = olMail.SentOn
        
        lastRow = lastRow + 1
    Next i
    
    'Clear all...
    Set olMail = Nothing
    Set olItems = Nothing
    Set olFolder = Nothing
    Set olNs = Nothing
    Set olApp = Nothing
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900