How to save emails to a specific folder on a network automatically using a macro

nathandavies

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
I have seen a thread this morning "Save Emails on Hard Drive" which interested me I was wondering if this code can be changed at all to complete the following?

1. When the macro is run a input box appears for a string of text to input the location of the folder on a server. IE Project Number
2. When the project number is inputted the macro will search through our server “P:\Group\JOBDATA” or “P:\Group\QUOTATION" and find the folder and save any incoming emails to a specific folder “Email.In” within the “Correspondence” Folder and “Email.Out” for outgoing emails.

I know this might be asking a lot but if anyone can help that would be greatly appreciated!!


Sub SaveMessages()

'Declaration
Dim myItems, myItem As Object
Dim myOrt As String
Dim myOlApp As New Outlook.Application
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim FindTerm(13)

'Set invalid characters to replace
FindTerm(0) = "*"
FindTerm(1) = "@"
FindTerm(2) = "\"
FindTerm(3) = "("
FindTerm(4) = ")"
FindTerm(5) = "["
FindTerm(6) = "]"
FindTerm(7) = "?"
FindTerm(8) = "<"
FindTerm(9) = ">"
FindTerm(10) = "!"
FindTerm(11) = "{"
FindTerm(12) = "}"
FindTerm(13) = ":"

'Ask for destination folder
myOrt = InputBox("Destination", "Save Attachments", "P:\")

On Error Resume Next

'work on selected items
Set myOlExp = myOlApp.ActiveExplorer
Set myOlSel = myOlExp.Selection

'for all items do...
For Each myItem In myOlSel

strdate = myItem.SentOn
newdate = Format(strdate, "yyyymmddhhmm")
strname = newdate & "-" & myItem.Subject & ".msg"

For i = 1 To 13
newstr = Replace(strname, FindTerm(i), " ")
strname = newstr
Next
myItem.SaveAs myOrt & newstr
myItem.Delete

Next

'free variables
Set myItems = Nothing
Set myItem = Nothing
Set myOlApp = Nothing
Set myOlExp = Nothing
Set myOlSel = Nothing

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#1 is possible, easy actually. #2 should be easy - sending the mail to the specific folder or creating the folder if it doesn't exist is easy, especially if it knows to use the Job folder or the quote folder. I don't have a macro that searches the hard drive for a folder.

The two macros on this page will save selected message and messages as they are sent. changing the folder path and inserting an input dialog to get the name are nothing.
 

nathandavies

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
thanks for your help, i'm very new to VBA so having difficulties with the code. can you point me in the right direction of a thread where it might have already been completed.
 

Andres B

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I'm also working on a similar project but mine has to actually download emails with an exact date or even within a date range if that's possible. the following is the code i have and it works but my issue is when outlook looks at the "received date" or the email it finds a random date and instead of increasing it does it in decreasing order.


Public Sub SaveAttachments()
Dim OlApp As Outlook.Application
Dim Inbox As MAPIFolder
Dim Item As Object
Dim ItemAttachments As Outlook.Attachments
Dim ItemAttachment As Object
Dim ItemAttCount As Long
Dim strFolderpath As String
Dim strFileName As String
Dim Counter As Long
Dim ItemsCount As Long
Dim ItemsAttachmentsCount As Long
Dim SaveFolder As String
Dim FileName As String
Dim searchDate As String, searchDate2 As String
Dim RcvDate As Date, SrchDate As Date

'chose a folder to save the attachments in
SaveFolder = BrowseForFolder("Select the folder you will like to save the attachments to.")
If SaveFolder = vbNullString Then Exit Sub

'input a date to search
searchDate = InputBox("Please enter a previous date to search from (mm/dd/yyyy)")
If searchDate = vbNullString Then Exit Sub

'searchDate2 = InputBox("to which date (mm/dd/yyyy/)")
'If searchDate2 = vbNullString Then Exit Sub


'convert string to date
SrchDate = CDate(searchDate)



' Instantiate an Outlook Application object.
Set OlApp = CreateObject("Outlook.Application")
Set Inbox = OlApp.ActiveExplorer.CurrentFolder

Counter = 1
ItemsCount = 0
ItemsAttachmentsCount = 0

For Each Item In Inbox.Items

'get the email's received date
RcvDate = Item.ReceivedTime

'compare the date the user entered with the received date
If SrchDate = RcvDate Then

ItemsCount = ItemsCount + 1

For Each ItemAttachment In Item.Attachments
ItemsAttachmentsCount = ItemsAttachmentsCount + 1

' Get the file name.
FileName = SaveFolder & "\" & ItemAttachment.FileName

' Save the attachment as a file.
ItemAttachment.SaveAsFile FileName

' Combine with the path to the Attachments folder.
StrFile = ItemAttachment.FileName & Counter & "_" & StrFile

ItemAttachment.Delete

Counter = Counter + 1
Next ItemAttachment
End If

Next Item

ExitSub:

Set ItemAttachment = Nothing
Set ItemAttachments = Nothing
Set Item = Nothing
Set Inbox = Nothing
Set OlApp = Nothing
MsgBox "All Selected Folder Attachments Have Been Downloaded ..."
MsgBox "ItemsCount : " & ItemsCount
MsgBox "ItemsAttachmentsCount : " & ItemsAttachmentsCount
End Sub




Also Another issue i have with this one is that when there is multiple attachments in one email only the first one is removed and not the others. Hope it helps and i hope anyone can help me with my issues as well since i'm also fairly new to VBA.

Thanks
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
1) See this example: http://www.vboffice.net/en/developers/how-many-appointments-do-you-have-today
It demonstrates how to sort the Items collection, and how to restrict for a certain date. Change the value for GetDefaultFolder to olFolderInbox, and replace the field names [start] and [end] by whatever you want to restrict for.

2) Since you remove items from the attachments collection, you need to loop backwards through it, which looks like this:
Code:
for i=count to 1 step-1
  'set attachment variable here
next
 

Andres B

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
1) See this example: http://www.vboffice.net/en/developers/how-many-appointments-do-you-have-today
It demonstrates how to sort the Items collection, and how to restrict for a certain date. Change the value for GetDefaultFolder to olFolderInbox, and replace the field names [start] and [end] by whatever you want to restrict for.

2) Since you remove items from the attachments collection, you need to loop backwards through it, which looks like this:
Code:
for i=count to 1 step-1
  'set attachment variable here
next

thanks, i tried that and its working now when i input a date to select emails for that date i used the same method but the problem now became that when i input a date that is 3 months back or 5 months back the macro runs but it doesn't extract the attachments form these emails. Do you know of any way why it is doing it?
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
It works for some dates, and it doesn't for other dates? Check if the Restrict function returns any item at all.
 

Andres B

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
It works for some dates, and it doesn't for other dates? Check if the Restrict function returns any item at all.
It's getting the same date but i think the problem is that now instead of item.receivedtime i'm using item.senton because once i put in the code for i = inbox.items.count 1 step to -1 the item.receivedtime property no longer is recognized.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
The kind of loop you're using has no effect on which properties are available. I guess you aren't setting the item variable in each loop.
 

Andres B

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
The kind of loop you're using has no effect on which properties are available. I guess you aren't setting the item variable in each loop.
The item variable is being set inside of the loop since i can use item.senton but nor item.receivedtime below is the loop in the code with the new requirements that were presented to me recently. Please let me know if you see where the problem might be happening or if you have any suggestions on improving the code. Thanks.


For i = Inbox.Items.Count To 1 Step -1

Set Item = Inbox.Items(i)
'i = 0

RcvDate = Format(Item.SentOn, "Short Date")

If RcvDate <= SrchDate Then

If SrchDate = RcvDate Then

For x = Item.Attachments.Count To 1 Step -1

Set Attach = Item.Attachments(x)

FileName = SaveFolder & "\" & Attach.FileName
Attach.SaveAsFile FileName
StrFile = Attach.FileName & ";" & StrFile
Attach.Delete

If Item.BodyFormat <> olFormatHTML Then
Item.Body = "The file(s) removed were: " & StrFile & vbCrLf & Item.Body
Else
Item.HTMLBody = "" & "The file(s) removed were: " & " " & StrFile & "<br><br>" & Item.HTMLBody
End If

Item.Save
StrFile = ""

Next x

Else
Exit Sub
End If
End If
Next i
 

Andres B

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
So what does mean?
I was thinking maybe that had to do something with the issues i'm having since item.senton is being used instead of the actual received time(Item.ReceivedTime) of the email itself. I'm just basing this on assumptions not really sure if it is the reason.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
I'm sorry, I don't understand what issues you're talking about.
 

Andres B

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I'm sorry, I don't understand what issues you're talking about.
The issues that i'm having is that when the for loop starts it seems to be starting on random emails picking random dates.. for instance if my last received e-mail is 12/09/2014 it will either pick that date and loop a few times and end up in a date like 12/03/2013 in only a few loops. It seems to just be choosing random email dates and searching from there.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Ok, see my first reply, which points to an example for how to sort the Items collection. You need to sort it to get the items in the order you want.
 

Top