2016-12-01 285 views
0

藉助以下編碼,我可以從Outlook收件箱中檢索數據並在Excel中更新它。 問題是我無法更新最新的響應,因爲宏先讀取先到先得更新的基礎。如果我從昨天收到abc的回覆,並且今天收到abc的更新回覆,那麼這個宏正在更新昨天的回覆。我們如何更改代碼以便宏應該從文件夾底部讀取電子郵件並更新所拉取的數據。Excel VBA代碼從底部的收件箱中讀取Outlook電子郵件

總之,我想更新我記錄中的最新回覆。

Dim outlookApp As Outlook.Application, oOutlook As Object 
Dim oInbox As Outlook.Folder, oMail As Outlook.MailItem 
Dim strAddress As String, strEntryId As String, getSmtpMailAddress As String 
Dim objAddressentry As Outlook.AddressEntry, objExchangeUser As Outlook.ExchangeUser 
Dim objReply As Outlook.MailItem, objRecipient As Outlook.Recipient 
Set outlookApp = New Outlook.Application 
Set oOutlook = outlookApp.GetNamespace("MAPI") 
Set oInbox = oOutlook.GetDefaultFolder(olFolderInbox) 

For Each oMail In oInbox.Items 

    If oMail.SenderEmailType = "SMTP" Then 
     strAddress = oMail.SenderEmailAddress 

    Else 
     Set objReply = oMail.Reply() 
     Set objRecipient = objReply.Recipients.Item(1) 
     strEntryId = objRecipient.EntryID 
     objReply.Close OlInspectorClose.olDiscard 
     strEntryId = objRecipient.EntryID 
     Set objAddressentry = oOutlook.GetAddressEntryFromID(strEntryId) 
     Set objExchangeUser = objAddressentry.GetExchangeUser() 
     strAddress = objExchangeUser.PrimarySmtpAddress() 
    End If 

    getSmtpMailAddress = strAddress 
    body = oMail.body 

回答

0

循環向後:

For i = oInbox.Count To 1 Step -1 
     If TypeName(oInbox.item(i)) = "MailItem" Then 
      Set oMail = oInbox.item(i) 
      'Do stuff here 
      Set oMail = Nothing 
     End If 
    Next i 
相關問題