Consulting

Results 1 to 4 of 4

Thread: Sort mails based on the title of the attachment name

  1. #1
    VBAX Newbie
    Joined
    Sep 2015
    Posts
    2
    Location

    Sort mails based on the title of the attachment name

    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 !

  2. #2
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Sep 2015
    Posts
    2
    Location
    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.


    Quote Originally Posted by gmayor View Post
    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.

  4. #4
    What EXACTLY are the subject text of the e-mails and the path of 'Y'
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •