简体   繁体   English

Excel Macro以保存Outlook 2010附件,将最早的电子邮件保存为最新的电子邮件

[英]Excel Macro to Save Outlook 2010 attachment, oldest email to newest email

Need to save excel attachments in Outlook emails from oldest email to newest email and mark email as read. 需要将Outlook电子邮件中的excel附件从最早的电子邮件保存到最新的电子邮件,并将电子邮件标记为已读。 The newer attachments will overwrite the older if there is more than one unread email. 如果有多个未读电子邮件,则较新的附件将覆盖较旧的附件。

I receive an number of emails daily that need to be saved to run a report. 我每天都会收到许多需要保存的电子邮件才能运行报告。 However, if one report is missed, it is ignored and I go to the next dataset. 但是,如果错过了一个报告,则将其忽略,然后转到下一个数据集。 The following works but does not always save the oldest first...it jumps around. 以下方法有效,但并不总是先保存最早的方法……它会跳来跳去。

I have tried a number of options to save oldest first, with no luck. 我尝试了多种选择来保存最早的文件,但是没有运气。 Any help on how I could make this consistently take the oldest email first. 关于如何使它始终如一地使用最早的电子邮件的任何帮助。 Thanks 谢谢

Sub Save_Attachments()
    Dim olApp As Outlook.Application, olNameSpace As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder, olMail As Outlook.MailItem
    Dim olAttachment As Outlook.Attachment, lngAttachmentCounter As Long
    Dim i As String
On Error GoTo Oooops
    Set olApp = New Outlook.Application
    Set olNameSpace = olApp.GetNamespace("MAPI")
    Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox).Folders("sub_folder")
    If olFolder Is Nothing Then Exit Sub
    For Each olMail In olFolder.Items
        If olMail.UnRead = True Then
             For Each olAttachment In olMail.Attachments
                lngAttachmentCounter = lngAttachmentCounter + 1
                olAttachment.SaveAsFile ThisWorkbook.Path & "\zzzzz.xls"
            Next olAttachment
        End If
        If olMail.UnRead Then
            olMail.UnRead = False
        End If
    Next olMail
    Exit Sub
Oooops:
    MsgBox Err.Description, vbExclamation, "An error occurred"
End Sub

Since you did not state the options you tried maybe you did not try 由于您没有陈述您尝试过的选项,所以也许您没有尝试过

For j = olFolder.Items.count To 1 Step -1 

Something like this. 这样的事情。

Option Explicit

 Sub Save_Attachments_ReverseOrder()

    Dim olApp As Outlook.Application, olNameSpace As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder
    Dim olMail As Object ' <-- olMail is not necessarily a mailitem
    Dim olAttachment As Outlook.attachment, lngAttachmentCounter As Long
    Dim j As Long

    On Error GoTo Oooops

    Set olApp = New Outlook.Application
    Set olNameSpace = olApp.GetNamespace("MAPI")
    Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox).Folders("sub_folder")

    If olFolder Is Nothing Then Exit Sub

    For j = olFolder.Items.count To 1 Step -1    

        Set olMail = olFolder.Items(j)
        If TypeOf olMail Is mailitem Then
            If olMail.UnRead = True Then

                Debug.Print olMail.subject & " - " & olMail.ReceivedTime

                 'For Each olAttachment In olMail.Attachments
                 '   lngAttachmentCounter = lngAttachmentCounter + 1
                 '   olAttachment.SaveAsFile ThisWorkbook.Path & "\zzzzz.xls"
                 'Next olAttachment

                 olMail.UnRead = False             

            Else

                Debug.Print vbCr & olMail.subject & " - " & olMail.ReceivedTime & " was previously read"

            End If     

        Else

            Debug.Print vbCr & "Current item is not a mailitem."      

        End If      

    Next j

    Exit Sub

Oooops:

    MsgBox Err.Description, vbExclamation, "An error occurred"

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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