Open Attachment with Outlook (VBA)

Coach3131

Board Regular
Joined
Nov 23, 2007
Messages
242
Each day I get an email that is autogenerated by a system at a specific time. I would like for Excel to search my email and open the file in the previous day's email... I have used rules for these specific emails to go into their own folder

How would I go about opening up emails and saving the attachments to a folder on my desktop? And how do I limit the date range so it doesn't want to open all my emails? I am able to do everything else I need after that, but need help with VBA in Excel to deal with Outlook.

I am unfamiliar with the objects using Outlook so this will be a good learning situation for me... I want to just open the attachment the attachment from the previous day and only emails with a specific subject name... any help would be greatly appreciated
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
This line is red and not usable

Dim WithEvents TargetFolderItems As Items

I tweaked the rest and can't test it yet though... don't understand this line

It says:
Compile Error

Only Valid in object module
 
Upvote 0
You can only declare a variable withevents within a class module, not in a normal module.
 
Upvote 0
How can I revise this one to only check for previous day's date and not the entire folder?

=====================================================

Public Sub GetAttachments()
Dim Inbox As MAPIFolder
Dim Item As MailItem
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer

Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders.Item("MyFolder")

For Each Item In Inbox.Items
For Each Atmt In Item.Attachments

FileName = "C:\" & Atmt.FileName
Atmt.SaveAsFile FileName

Next

Next

Set Inbox = Nothing
End Sub
 
Upvote 0
Add a check to see if Item.SentOn >= Date
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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