2017-08-27 20 views
0

請參考下面的代碼。該代碼將在Excel VBA中,然後運行它以便在Outlook中提取大量電子郵件。我有第二個問題,需要你的幫助。假設我的「收件箱」有大約400多封電子郵件。現在。下面的「對於每個下一個」代碼開始查看最古老的電子郵件!那麼如果代碼從最新版本開始,我該如何更改代碼呢?對於每一個下一個代碼,我怎樣才能使代碼從最新開始到最早?

下面是代碼:

Sub GetFromInbox() 

    Dim olApp As Outlook.Application 
    Dim olNs As Namespace 
    Dim Fldr As MAPIFolder 
    Dim olMail As Variant 
    Dim i, ij As Integer 
    Dim tt As Date 

    Set olApp = New Outlook.Application 
    Set olNs = olApp.GetNamespace("MAPI") 
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox) 
    i = 1 
    ij = 0 
    x = Date 

    ' Now. the following "For each next " code starts to look in the oldest email! 
    ' So how can I change the code if the code starts from the newest? 
    For Each olMail In Fldr.Items 
     ij = ij + 1 
     'If IsNumeric((Format(olMail.ReceivedTime, "dd/mm/yy"))) Then 
      Sheets("test").Range("a1").Select 
      Sheets("test").Range("I1").Clear 
      Sheets("test").Range("I2") = ij 
      Sheets("test").Range("I1").Value = (Format(olMail.ReceivedTime, "dd/mm/yy")) 
      Sheets("test").Range("I1").NumberFormat = "dd/mm/yy" 
      tt = Sheets("test").Range("I1") 
      ' MsgBox ("Y-tt=" & tt & " receivedtime=" & olMail.ReceivedTime) 
     'Else 
      'tt = 0 
      'MsgBox ("N-tt=" & tt & " receivedtime=" & olMail.ReceivedTime) 
     'End If 
     ' tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy")) 
     If tt >= Range("H1") Then 
      'If InStr(olMail.Subject, "others") > 0 And tt >= Range("h1") Then 
      If InStr(olMail.Subject, "others") > 0 Then 
       ActiveSheet.Range("h2") = "y" 
       ActiveSheet.Cells(i, 1).Value = olMail.Subject 
       ActiveSheet.Cells(i, 2).Value = olMail.ReceivedTime 
       ActiveSheet.Cells(i, 3).Value = olMail.SenderName 
       tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy")) 
       ActiveSheet.Cells(i, 4).Value = CDate(Format(olMail.ReceivedTime, "dd/mm/yy")) 
       ' tt = ActiveSheet.Cells(i, 4).Value 
       ActiveSheet.Cells(i, 5).Value = (Format(olMail.ReceivedTime, "hh:mm")) 
       MsgBox ("tt=" & tt) 
       i = i + 1 
      End If 
     Else 
      Sheets("test").Range("h2") = "N" 
     End If 
    Next olMail 

    Set Fldr = Nothing 
    Set olNs = Nothing 
    Set olApp = Nothing 
    'tt = "" 

End Sub 
+0

如何嘗試'For i = Fldr.Items.Count to 1 Step -1'? – Porcupine911

+0

olmail呢? olmail是For Each Next代碼中的主要功能!上面的代碼 - 我通過互聯網搜索它,然後更改/更新我的情況!據我瞭解,olmail是指「收件箱」中的每封郵件!所以你的想法可能無法正常工作,但我不確定!請告訴我,如果我錯了 – Bnf

+0

在這種情況下,你會用'Fldr.Items(i)'替換'olMail'。由於您已經在使用'i',因此您可以用另一個變量名稱替換它(如「j'」)。 – Porcupine911

回答

0

排序的項目,以確保訂單的。

Sub ItemsInReliableOrder() 

    ' https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/items-sort-method-outlook 

    Dim olApp As Outlook.Application 
    Dim olNs As Namespace 

    ' 2007 and subsequent 
    Dim Fldr As Folder 

    ' Allows for any type of item in folder 
    Dim myItem As Object 
    Dim myItems As Outlook.Items 

    Set olApp = New Outlook.Application 
    Set olNs = olApp.GetNamespace("MAPI") 
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox) 

    Set myItems = Fldr.Items 

    myItems.Sort "[ReceivedTime]", True 

    For Each myItem In myItems 

     If myItem.class = olMail Then 
      Debug.Print myItem.Subject & "-- " & myItem.ReceivedTime 
     Else 
      Debug.Print "Not a mailitem." 
     End If 

    Next myItem 

    Set Fldr = Nothing 
    Set olNs = Nothing 
    Set olApp = Nothing 
    Set myItem = Nothing 
    Set myItems = Nothing 

End Sub 

不要直接對Fldr.Items進行排序,設置項目集合然後排序。避免使用olMail作爲它已經有意義的變量名稱。

相關問題