I'm trying to download and then open an Excel spreadsheet attachment in an Outlook email using VBA in Excel. How can I:
- Download the one and only attachment from the first email (the newest email) in my Outlook inbox
- Save the attachment in a file with a specified path (eg: "C:...")
- Rename the attachment name with the: current date + previous file name
- Save the email into a different folder with a path like "C:..."
- Mark the email in Outlook as "read"
- Open the excel attachment in Excel
I also want to be able to save the following as individual strings assigned to individual variables:
- Sender email Address
- Date received
- Date Sent
- Subject
- The message of the email
although this may be better to ask in a separate question / look for it myself.
The code I do have currently is from other forums online, and probably isn't very helpful. However, here are some bits and pieces I have been working on:
Sub SaveAttachments()
Dim olFolder As Outlook.MAPIFolder
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim fsSaveFolder As String
fsSaveFolder = "C:\test\"
strFilePath = "C:\temp\"
Set olFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
For Each msg In olFolder.Items
While msg.Attachments.Count > 0
bflag = False
If Right$(msg.Attachments(1).Filename, 3) = "msg" Then
bflag = True
msg.Attachments(1).SaveAsFile strFilePath & strTmpMsg
Set msg2 = Application.CreateItemFromTemplate(strFilePath & strTmpMsg)
End If
sSavePathFS = fsSaveFolder & msg2.Attachments(1).Filename
End If
End Sub
I can give you the complete code in one go but that wouldn't help you learn from it ;) So let's Break up your requests and then we will tackle them 1 by 1. This is gonna be a very long post so be patient :)
There are total 5 parts which will cover all 7 (yes 7 and not 6) points so you don't have to create a new question for your 7th point.
PART - 1
Sender email Address
,Date received
,Date Sent
,Subject
,The message of the email
See this code example. I am latebinding with Outlook from Excel then checking if there are any unread items and if there are I am retrieving the relevant details.
So that take care of your request which talks about storing details in the variables.
PART - 2
Now moving on to your next request
See this code example. I am again latebinding with Outlook from Excel then checking if there are any unread items and if there are I am further checking if it has an attachment and if it has then download it to the relevant folder.
PART - 3
Moving on to your next request
See this code example. This save the email to say C:\
PART - 4
Moving on to your next request
See this code example. This will mark the email as
read
.PART - 5
Moving on to your next request
once you have downloaded the file/attachment as shown above then use that path in the below code to open the file.
Thanks to Sid :) for your code(stolen your code) .. i had this situation today .Here is my code .below code saves attachement,mail also mail information ..All credits goes to Sid