2016-12-01 291 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 
相关问题