Search for email using cell value and attach referenced file

scotsrule08

New Member
Joined
Jun 21, 2018
Messages
45
Here’s a doozy!

I have a spreadsheet with reference numbers and the path to a spreadsheet for each reference number.

I would like to search through Outlook for an email with the subject line that includes the reference number, then with that email reply with the specific referenced spreadsheet. Then loop through all of the cells.

The reference numbers are in column A and the corresponding file path is in column B.

Any help is appreciated Ladies & Gents!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi,

You would need to go in vba/tools/references... and tick Microsoft Outlook 16.0 (or any version) object library to use outlook from Excel. Keep in mind that this step is required again if you change computer.

This is untested but that is the structure I would use:

Code:
Sub LoopEmails()


Dim objNS As Outlook.Namespace: Set objNS = GetNamespace("MAPI")
Dim olFolder As Outlook.MAPIFolder
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Dim Item As Object
Dim ref As Range
Dim sht As Worksheet
Set sht = ActiveSheet
Dim LastRow As Long
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Dim olReply As MailItem


For Each Item In olFolder.Items
    If TypeOf Item Is Outlook.MailItem Then
        Dim oMail As Outlook.MailItem: Set oMail = Item
        For Each ref In Range("A1:A" & LastRow)
            If InStr(1, oMail.Subject, ref.Value) <> 0 Then
                Set olReply = oMail.ReplyAll
                olReply.HTMLBody = "Hello, Here is a path: " & ref.Offset(0, 1).Value & vbCrLf & oMail.HTMLBody
                olReply.Display '.send to send without display
            End If
        Next ref
    End If
Next Item


End Sub
 
Upvote 0
Hi,

I tested it and basically had an issue with empty cell (it replies to all mails then), so I would rather use this one

Code:
Sub LoopEmails()


Dim objNS As Outlook.Namespace: Set objNS = GetNamespace("MAPI")
Dim olFolder As Outlook.MAPIFolder
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Dim Item As Object
Dim ref As Range
Dim sht As Worksheet
Set sht = ActiveSheet
Dim LastRow As Long
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Dim olReply As MailItem


For Each Item In olFolder.Items
    If TypeOf Item Is Outlook.MailItem Then
        Dim oMail As Outlook.MailItem: Set oMail = Item
        For Each ref In Range("A1:A" & LastRow)
           [COLOR=#006400]'Check reference cell is not empty and included in mail subject[/COLOR]
           [COLOR=#0000ff] If (ref.value<>"" and InStr(1, oMail.Subject, ref.Value) <> 0)[/COLOR] Then
                Set olReply = oMail.ReplyAll
                olReply.HTMLBody = "Hello, Here is a path: " & ref.Offset(0, 1).Value & vbCrLf & oMail.HTMLBody
                olReply.Display '.send to send without display
            End If
        Next ref
    End If
Next Item


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,525
Members
452,651
Latest member
wordsearch

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