从outlook收到电子邮件date。 单元格中信息格式和search的问题

Sub ReceivedEmailDate() Dim outlookApp Dim olNs As Outlook.Namespace Dim Fldr As Outlook.MAPIFolder Dim olMail As Variant Dim myTasks Dim sir() As String Set outlookApp = CreateObject("Outlook.Application") Set olNs = outlookApp.GetNamespace("MAPI") Set Fldr = olNs.GetDefaultFolder(olFolderInbox) Set myTasks = Fldr.Items i = 2 'This is the row you want the date/time to start from For Each olMail In myTasks 'This is where the search occurs in the outlook inbox (parameters can be altered depending on what you want to search for) 'I have stated to search for the invoice numbers in the subject field of the email. Invoice numbers located down column E If (InStr(1, olMail.Subject, ActiveSheet.Cells(i, 5), vbTextCompare) > 0) Then ActiveSheet.Cells(i, 8).Value = Format(olMail.ReceivedTime, "DD/MM/YY") 'format function to only show the date, rather than both date and time i = i + 1 'Count so that each email date/time is entered in the row below the previous one End If Next olMail Set Fldr = Nothing Set olNs = Nothing Set olApp = Nothing End Sub 

我希望代码在outlook中searchE2中的内容,并在H2单元格中发送接收date,然后在outlook中search单元格E3中的内容,并在单元格H3中发送接收date等。

我以为我已经工作了一段时间,但现在只是在第一个单元格(H2)。 此外,格式似乎遍布各地。

任何帮助将非常感激。

没有循环遍历E列中的值。另一个For ... Next循环涉及到i应该足够了。

 Sub ReceivedEmailDate() Dim outlookApp Dim olNs As Outlook.Namespace Dim Fldr As Outlook.MAPIFolder Dim olMail As Variant Dim myTasks Dim sir() As String Set outlookApp = CreateObject("Outlook.Application") Set olNs = outlookApp.GetNamespace("MAPI") Set Fldr = olNs.GetDefaultFolder(olFolderInbox) Set myTasks = Fldr.Items With ActiveSheet For i = 2 To .Cells(Rows.Count, "E").End(xlUp).Row For Each olMail In myTasks 'This is where the search occurs in the outlook inbox (parameters can be altered depending on what you want to search for) 'I have stated to search for the invoice numbers in the subject field of the email. Invoice numbers located down column E If CBool(InStr(1, olMail.Subject, .Cells(i, "E").Value, vbTextCompare)) Then .Cells(i, "H") = CDate(olMail.ReceivedTime) 'put a real datetime in whether you need it right now or not .Cells(i, "H").NumberFormat = "dd/mm/yy" 'format function to only show the date, rather than both date and time End If Next olMail Next i End With Set myTasks = Nothing Set Fldr = Nothing Set olNs = Nothing Set outlookApp = Nothing End Sub 

我已经把一个真正的date时间放到H列,并格式化为dd / mm / yy 。 如果您愿意,可以恢复为date的文本表示forms。