2013-08-01 46 views
1

我有下面的宏用于保存附件,它工作正常,但我希望它将电子邮件移动到另一个文件夹一旦附件已保存。任何帮助将非常感激!宏以移动选定的Outlook电子邮件

Option Explicit 

Public Sub SaveFolderAttachments() 

    ' Ask the user to select a file system folder for saving the attachments 
    Dim oShell As Object 
    Set oShell = CreateObject("Shell.Application") 
    Dim fsSaveFolder As Object 
    Set fsSaveFolder = oShell.BrowseForFolder(0, "Please Select a Save Folder:", 1) 
    If fsSaveFolder Is Nothing Then Exit Sub 
    ' Note: BrowseForFolder doesn't add a trailing slash 

    ' Ask the user to select an Outlook folder to process 
    Dim olPurgeFolder As Outlook.MAPIFolder 
    Set olPurgeFolder = Outlook.GetNamespace("MAPI").PickFolder 
    If olPurgeFolder Is Nothing Then Exit Sub 

    ' Iteration variables 
    Dim msg As Outlook.MailItem 
    Dim att As Outlook.Attachment 
    Dim sSavePathFS As String 
    Dim sDelAtts 

    For Each msg In olPurgeFolder.Items 

    sDelAtts = "" 

    ' We check each msg for attachments as opposed to using .Restrict("[Attachment] > 0") 
    ' on our olPurgeFolder.Items collection. The collection returned by the Restrict method 
    ' will be dynamically updated each time we remove an attachment. Each update will 
    ' reindex the collection. As a result, it does not provide a reliable means for iteration. 
    ' This is why the For Each loops will not work. 
    If msg.Attachments.Count > 0 Then 

     ' This While loop is controlled via the .Delete method 
     ' which will decrement msg.Attachments.Count by one each time. 
     While msg.Attachments.Count > 0 

     ' Save the file 
     sSavePathFS = fsSaveFolder.Self.Path & "\" & msg.Attachments(1).FileName 
     msg.Attachments(1).SaveAsFile sSavePathFS 

     ' Build up a string to denote the file system save path(s) 
     ' Format the string according to the msg.BodyFormat. 
     If msg.BodyFormat <> olFormatHTML Then 
      sDelAtts = sDelAtts & vbCrLf & "<file://" & sSavePathFS & ">" 
     Else 
      sDelAtts = sDelAtts & "<br>" & "<a href='file://" & sSavePathFS & "'>" & sSavePathFS & "</a>" 
     End If 

     ' Delete the current attachment. We use a "1" here instead of an "i" 
     ' because the .Delete method will shrink the size of the msg.Attachments 
     ' collection for us. Use some well placed Debug.Print statements to see 
     ' the behavior. 
     msg.Attachments(1).Delete 

     Wend 

     ' Modify the body of the msg to show the file system location of 
     ' the deleted attachments. 
     If msg.BodyFormat <> olFormatHTML Then 
     msg.Body = msg.Body & vbCrLf & vbCrLf & "Attachments Deleted: " & Date & " " & Time & vbCrLf & vbCrLf & "Saved To: " & vbCrLf & sDelAtts 
     msg 
     Else 
     msg.HTMLBody = msg.HTMLBody & "<p></p><p>" & "Attachments Deleted: " & Date & " " & Time & vbCrLf & vbCrLf & "Saved To: " & vbCrLf & sDelAtts & "</p>" 
     End If 

     ' Save the edits to the msg. If you forget this line, the attachments will not be deleted. 
     msg.Save 

    End If 

    Next 

End Sub 

回答

1

调用MailItem.Move(MAPIFolder)来移动消息。不要使用“每个”循环,如果蜉移动消息(因为收藏数变化),使用一个下行循环(对于I = Items.Count 1步-1)

编辑:

Dim objItems as Outlook.Items 
set objItems = olPurgeFolder.Items 
for I = objItems.Count to 1 step -1 
    set msg = objItems.Item(i) 
+0

你认为你可以修改我的代码来包含这个吗? –

+1

查看上面更新的答案 –

+0

非常感谢!最后一个问题我应该把它放到我的代码中吗?因为我目前正在得到一个关于“I”在“for I = objItems.Count to 1 step -1”行中? –

相关问题