Rule to move attachments with specific name

Nadine

New Member
Outlook version
Outlook 2016 32 bit
Email Account
POP3
Please can someone assist me with VBA Code to create a rule for outlook to move all emails with the attachment name "SALESINVOICE" to a sub folder in my Inbox Called "Invoices". It is a pop account. Thank you.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
The macros on Use VBA to move messages with attachments comes close to what you need - the first runs in a rule, the second checks every message that is added to the folder.

This checks for the filetype - you'll need to change it to check the filename
Code:
For i = attCount To 1 Step -1
      strFile = Item.Attachments.Item(i).FileName
      
      sFileType = LCase$(Right$(strFile, 4))
    
    Select Case sFileType
        Case ".pdf", ".doc", "docx"
    ' do something if the file types are found 
    ' this code moves the message
      Item.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Invoices"))
    
   ' stop checking if a match is found and exit sub
       GoTo endsub
      End Select
  Next i


This should work but i didn't test it. I don't guarantee its error free. :) if SALESINVOICE will always be upper case, you can use it, but if the case can vary, you need to convert the subject to lower caser (or upper case) when checking. I prefer to use lcase: if instr(lcase(strfile), "salesinvoice") > 0 then

Code:
For i = attCount To 1 Step -1
      strFile = Item.Attachments.Item(i).FileName
      
if instr(strfile, "SALESINVOICE") > 0 then 
    ' do something if the file types are found 
    ' this code moves the message
      Item.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Invoices"))
    
   ' stop checking if a match is found and exit sub
       GoTo endsub
end if
  Next i
 
Top