简体   繁体   中英

Outlook rule + VBA: Save attachment then move message to selected folder

I need to use rule for some specific mails I'm recieving. I have to save attachment to specific folder IE D:\\TEST) then move message to specific subfolder in ANOTHER FILE mailbox file attached to Outlook.

I found and adapt code for my needs but outlook rules are "numb" and there is no possibility to change order of rules step execute:

If msg hit rule:

Move MSG to specific folder

Then Run script

So in conclusion a script didn't find a MSG because it's moved by rule (I can't find an option to reverse order and get:

If msg hit rule:

Run script

Then Move MSG to specific folder So solution is to write a script and attach when rule is triggered.

Public Sub SaveAttach(Item As Outlook.MailItem)

If Item.Attachments.Count > 0 Then

 Dim objAttachments As Outlook.Attachments
 Dim lngCount As Long
 Dim strFile As String
 Dim sFileType As String
 Dim strFolderpath As String
 Dim i As Long
 Dim myDestFolder As Outlook.Folder
 Set myDestFolder = myInbox.Folders("Mails").Folders("Exported")

 Set objAttachments = Item.Attachments
     lngCount = objAttachments.Count
   For i = lngCount To 1 Step -1
     strFile = objAttachments.Item(i).FileName
     strFolderpath = "D:\TEMP\"
     strFile = strFolderpath & strFile
     objAttachments.Item(i).SaveAsFile strFile
   Next i

Item.Move myDestFolder

End If
End Sub

Above code putted to ThisOutlookSession and then connected to rule "run script" works, but how to expand this code above to move MSG who attachment I extracted to another outlook subfolder in IE Mails\\Exported\\ ?

edit:

Added lines

Dim myDestFolder As Outlook.Folder

Set myDestFolder = myInbox.Folders("Mails").Folders("Exported")

Item.Move myDestFolder

But still something is wrong and VBA at this mode (run script as part of rule) dont run debug and cant see error message, it's just not work (i can only set msgbox "Im here" to track what part of script wont work :/

A tree of files looks like this

1 Inbox(DefaultIncomingMailsFile.ost (IMAP))

2 Mails(LocalFile.pst)

2a Exported (subfolder in Mails where should be moved mails after attachment extraction)

but how to expand this code above to move MSG who attachment I extracted to another outlook subfolder in IE Mailbox\\Done\\ ?

You need to use the Move method of the MailItem class which moves a Microsoft Outlook item to a new folder.

Public Sub SaveAttach(Item As Outlook.MailItem)

  If Item.Attachments.Count > 0 Then
   Dim objAttachments As Outlook.Attachments
   Dim lngCount As Long
   Dim strFile As String
   Dim sFileType As String
   Dim strFolderpath As String
   Dim i As Long

   Set objAttachments = Item.Attachments
   lngCount = objAttachments.Count
   For i = lngCount To 1 Step -1
     strFile = objAttachments.Item(i).FileName
     strFolderpath = "D:\TEMP\"
     strFile = strFolderpath & strFile
     objAttachments.Item(i).SaveAsFile strFile
   Next i

   Item.Move yourTargetFolder

  End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM