Log in

View Full Version : Sort mails based on the title of the attachment name



freeb0
09-27-2015, 12:19 AM
Hello guys

I found some vba code to move a message based on the type of attachment, but I would like to change it to the title of the attachment based on the date of today and tomorrow formatted yyymmdd.
Do you know how I could adapt the code below to do just that ? Every mail normally has just one attachment so there is no need for a loop.
The date is always on the exact same place in the attachment file name.

Sub MoveMail(Item As Outlook.MailItem)

If Item.Attachments.Count > 0 Then

Dim attCount As Long
Dim strFile As String
Dim sFileType As String
Dim sFileName As String
Dim sDateplus As String


sDateplus = Format(Date, "yyyymmdd")
sDateplus = DateAdd("dd", 1, Date)
'to check if it works
Debug.Print "date" & intDateplus

attCount = Item.Attachments.Count

For i = attCount To 1 Step -1
strFile = Item.Attachments.Item(i).FileName
'here it checks the entire attachment name with the date
sFileName = Right$(strFile, 20)


Select Case sFileName
Case "sDateplus"
' do something if the file types are found
' this code moves the message to folder attachments
Item.Move (Session.GetDefaultFolder(olattachments).Folders("Move"))

' stop checking if a match is found and exit sub
GoTo endsub
End Select
Next i

End If


Set Item = Nothing

End Sub

I have been searching a lot, but my outlook vba skills are very bad and I couldn't make it to work.

Thank you very much !

gmayor
09-27-2015, 02:22 AM
It is difficult to work out from your code (and your explanation) what it is that you are trying to do.

The macro starts with

sDateplus = Format(Date, "yyyymmdd")
sDateplus = DateAdd("dd", 1, Date)
'to check if it works
Debug.Print "date" & intDateplus
The macro does not make clear what intDateplus refers to, and if you want to add 1 day to the current date then
sDateplus = Format(Date + 1, "yyyymmdd")
might be closer to what you had in mind, but how this date format relates to the message or any attachment is unclear.

There is no folder olattachments
Session.GetDefaultFolder(olattachments).Folders("Move") so it is uncertain what you are trying to do there. Maybe

Session.GetDefaultFolder(olInbox).Folders("Attachments").Folders("Move")If you add Option Explicit to the top of the module it will highlight some of the more obvious issues, but a clearer explanation of what you are trying to do (and when) would be more helpful.

freeb0
09-27-2015, 05:48 AM
Ok, so the general goal is to have the same functionality as outlook rules.
When e-mail arrives with x in subject -> move it to map Y
The only difference that I would like to do is
When e-mail arrives with x in name of attachment -> move it to map Y (of account Z)
x is based on the date of tomorrow because every day 3 new emails arrive with the same attachment title but with different dates in that attachment title. I only need the one with the date of tomorrow to be moved to another map.

Thanks for your help.



It is difficult to work out from your code (and your explanation) what it is that you are trying to do.

The macro starts with

sDateplus = Format(Date, "yyyymmdd")
sDateplus = DateAdd("dd", 1, Date)
'to check if it works
Debug.Print "date" & intDateplus
The macro does not make clear what intDateplus refers to, and if you want to add 1 day to the current date then
sDateplus = Format(Date + 1, "yyyymmdd")
might be closer to what you had in mind, but how this date format relates to the message or any attachment is unclear.

There is no folder olattachments
Session.GetDefaultFolder(olattachments).Folders("Move") so it is uncertain what you are trying to do there. Maybe

Session.GetDefaultFolder(olInbox).Folders("Attachments").Folders("Move")If you add Option Explicit to the top of the module it will highlight some of the more obvious issues, but a clearer explanation of what you are trying to do (and when) would be more helpful.

gmayor
09-27-2015, 09:04 PM
What EXACTLY are the subject text of the e-mails and the path of 'Y'