Open email by attachment name

Mylarbi

New Member
Joined
Feb 9, 2020
Messages
48
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi, I am trying to get a macro that
1. Searches for and based on file name of Excel attachment.
2. The file name is the value of the active cell.
2. Opens the email found.
3. Saves and Opens the Excel attachment.
4. Closes the email.

I have put the following code together but it doesn't work as expected. Thanks in advance.

VBA Code:
Sub Search_Email_Open()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim strTempFilePath As String
Dim sFileName As String
Dim atchName As String
Dim i As Integer

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("Reports")
atchName = ActiveCell.Value
i = 1

For Each olMail In Fldr.Items
If atchName = sFileName Then
olMail.Display

sFileName = olMail.Attachments.Item(1).DisplayName
    strTempFilePath = "C:\Users\User1\Documents\"
    olMail.Attachments(1).SaveAsFile sFileName
    olMail.Close olSave
    Workbooks.Open strTempFilePath & sFileName
i = i + 1
End If

Next olMail
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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