0

I've got an Outlook folder with saved mails, each containing a list of tickers. I want to be able to search for a ticker and get the date of the most recent mail containing that specific ticker. I did try searching for solutions online such as the one here Excel VBA for searching in mails of Outlook , but I've been unable to find something which suits my needs.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Srikanth
  • 29
  • 7

1 Answers1

1

Try this user defined function that I've written for you. Add this code to a module in excel then use it as a normal function in Excel. You can input the ticker name directly into the function in text quotes, e.g. =FindTicker("ABC"), or you can reference another cell that has the ticker value, e.g. FindTicker(A1)

I'm assuming you've saved the emails to a subfolder of your inbox. In the code, replace "Subfolder name" with the name of your subfolder (still in double quotes). If the emails haven't been moved from your inbox, then remove that line entirely.

For this to work, you need to add a reference to the Microsoft Outlook Reference Library.

This function will only operate in the workbook where you add the code, unless you save the workbook as an add-in and then install it, in which case the function will be available in any workbook.

Public Function FindTicker(strTicker As String) As Variant

Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olFolderItems As Outlook.Items
Dim olMail As Outlook.MailItem

'Open outlook if it's not open, otherwise connect to open instance
If Outlook.Application.Explorers.Count = 0 Then
    Set olApp = CreateObject("Outlook.Application")
Else
    Set olApp = Outlook.Application
End If

'Get emails to search through
Set olNamespace = olApp.GetNamespace("MAPI")
Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("Subfolder name") 'UPDATE/REMOVE THIS LINE
Set olFolderItems = olFolder.Items

'Set default value if not found
FindTicker = "Ticker not found"

'Search through bodies of emails for ticker value
For Each olMail In olFolderItems
    If (InStr(1, olMail.Body, strTicker, vbTextCompare) > 0) Then
        FindTicker = Format(olMail.SentOn, "dd/mm/yyyy")
        Exit For
    End If
Next

'Clear object variables
Set olApp = Nothing
Set olNamespace = Nothing
Set olFolder = Nothing
Set olFolderItems = Nothing
Set olMail = Nothing

End Function
Michael
  • 4,563
  • 2
  • 11
  • 25
  • Thanks much for your response. I tried your code and this line fails: Set olFolder = olFolder.Folders("Search Tool"). I have that folder within the Inbox folder. – Srikanth Jan 27 '17 at 11:23
  • Is it a subfolder of the Inbox, or is it a subfolder of another subfolder in the inbox? – Michael Jan 27 '17 at 11:24
  • It is a folder within Inbox. I also have an Inbox in an archive, and I guess that's the default. When I move the sub-folder into the Inbox of the archive, the code works fine! Thank you very much! – Srikanth Jan 27 '17 at 11:30
  • Can you please also let me know what error you're getting? I tried misnaming a folder and I didn't get a code error. Instead, the function just returned an error. – Michael Jan 27 '17 at 11:32
  • Oh good job getting it to work. I couldn't have debugged that issue from my side! You're welcome. – Michael Jan 27 '17 at 11:33
  • Just to know..say the folder is in the same level as Inbox, under the email account name, like abc@xyz.com contains Inbox, Sent, ...., Search Tool. Then, how would I refer to the folder in that case? – Srikanth Jan 27 '17 at 11:37
  • Everything is nested sub-folders of the namespace. The inbox is the 2nd level down. So in this case: olnamespace.folders("abc@xyz.com").folders("Search Tool"). You can refer to folders by index or by name. Try putting a break in the code, reenter a formula then use the immediate window to navigate through the folder from the namespace while the object variables are set: print olnamespace.folders(1), print olnamespace.folders(1).folders(1), etc You can also work back up from your default inbox using .parent: olnamespace.getdefaultfolder(olfolderinbox).parent.folders("Search Tool") – Michael Jan 27 '17 at 11:50
  • Got it! Works great! Thank you very much :) – Srikanth Jan 27 '17 at 13:17
  • 1
    Don't ever loop through all items in a folder. Use Items.Find/FindNext or Items.Restrict. – Dmitry Streblechenko Jan 27 '17 at 16:37