从 outlook 收到电子邮件日期。格式问题和从单元格中的信息搜索

Received email date from outlook. Problems with format and search from info in cells

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 中搜索单元格 E2 中的内容和 post 单元格 H2 中的接收日期,然后在 outlook 中搜索单元格 E3 中的内容和 post单元格 H3 中的接收日期等等。

我以为我让它工作了一段时间,但现在只在第一个单元格 (H2) 中 posts。此外,格式似乎无处不在。

如有任何帮助,我们将不胜感激。

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

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

我已将真实的日期时间放入 H 列并将其格式化为 dd/mm/yy。如果愿意,您可以恢复为日期的文本表示形式。