Vba 宏以移动选定的outlook电子邮件

Vba 宏以移动选定的outlook电子邮件,vba,outlook,outlook-2010,Vba,Outlook,Outlook 2010,我有以下用于保存附件的宏,它工作正常,但我希望它在保存附件后将电子邮件移动到另一个文件夹。任何帮助都将不胜感激 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.Applicat

我有以下用于保存附件的宏,它工作正常,但我希望它在保存附件后将电子邮件移动到另一个文件夹。任何帮助都将不胜感激

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
选项显式
公共子存储文件夹附件()
'要求用户选择用于保存附件的文件系统文件夹
将oShell作为对象
设置oShell=CreateObject(“Shell.Application”)
将文件夹设置为对象
设置fsSaveFolder=oShell.BrowseForFolder(0,“请选择一个保存文件夹:”,1)
如果fsSaveFolder为Nothing,则退出子文件夹
'注意:BrowseForFolder不添加尾部斜杠
'要求用户选择要处理的Outlook文件夹
将olPurgeFolder设置为Outlook.MAPIFolder
设置olPurgeFolder=Outlook.GetNamespace(“MAPI”).PickFolder
如果olPurgeFolder为Nothing,则退出Sub
'迭代变量
将消息作为Outlook.mailtim
将att调暗为Outlook。附件
将sSavePathFS设置为字符串
暗咖啡
对于olPurgeFolder.Items中的每个msg
sDelAtts=“”
'我们检查每个消息的附件,而不是使用.Restrict(“[Attachment]>0”)
'在我们的olPurgeFolder.Items集合中。Restrict方法返回的集合
'将在每次删除附件时动态更新。每次更新都将
'重新为集合编制索引。因此,它不能为迭代提供可靠的方法。
这就是为什么For-Each循环不起作用的原因。
如果msg.Attachments.Count>0,则
'此While循环通过.Delete方法控制
'这将使msg.Attachments.Count每次减少一个。
而msg.Attachments.Count>0
'保存文件
sSavePathFS=fsSaveFolder.Self.Path&“\”msg.Attachments(1).FileName
msg.Attachments(1).SaveAsFile sSavePathFS
'建立一个字符串以表示文件系统保存路径
'根据msg.BodyFormat格式化字符串。
如果msg.BodyFormat olFormatHTML,则
sDelAtts=sDelAtts&vbCrLf&“
其他的
sDelAtts=sDelAtts&“
”和” 如果结束 '删除当前附件。这里用“1”代替“i” '因为.Delete方法将缩小msg.Attachments的大小 “为我们收集。使用一些位置合适的Debug.Print语句查看 "行为。 msg.附件(1).删除 温德 '修改消息体以显示消息的文件系统位置 '删除的附件。 如果msg.BodyFormat olFormatHTML,则 msg.Body=msg.Body&vbCrLf&vbCrLf&“附件已删除:”&Date&&Time&vbCrLf&vbCrLf&“保存到:”&vbCrLf&sDelAtts 味精 其他的 msg.HTMLBody=msg.HTMLBody&“”&“附件已删除:&Date&&Time&vbCrLf&vbCrLf&”保存到:&vbCrLf&sDelAtts&“

” 如果结束 '保存对消息的编辑。如果忘记此行,附件将不会被删除。 味精。保存 如果结束 下一个 端接头
调用MailItem.Move(MAPIFolder)移动邮件。不要使用“for each”循环如果我移动消息(因为集合计数更改),请使用向下循环(对于I=Items.count到1步骤-1)

编辑:

调用MailItem.Move(MAPIFolder)移动邮件。如果要移动邮件,请不要使用“for each”循环(因为集合计数已更改),请使用向下循环(对于I=Items.count,请使用1步骤-1)

编辑:


非常感谢!最后一个问题,我应该把它放在我的代码中的什么地方?由于我目前在“for I=objItems.Count to 1 step-1”行中得到一个关于“I”的未定义变量错误?请在函数顶部添加“dim I”,谢谢!最后一个问题,我应该把它放在我的代码中的什么地方?由于我当前在“for I=objItems.Count to 1 step-1”行中遇到一个变量未定义的错误,请在函数顶部添加“dim I”
Dim objItems as Outlook.Items 
set objItems = olPurgeFolder.Items
for I = objItems.Count to 1 step -1
  set msg = objItems.Item(i)