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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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