【问题标题】:Save date and time of emails into Excel sheet based on email id for last 30 days根据过去 30 天的电子邮件 ID 将电子邮件的日期和时间保存到 Excel 表中
【发布时间】:2018-02-27 07:55:53
【问题描述】:

这是我的代码,我对过滤日期感到震惊。我需要保存从现在起最近 30 天的 User(harshahowrang@gmail.com) 电子邮件的日期和时间信息。必须将该信息(时间和日期)保存在 Excel 工作表中

Set ObjO = CreateObject("Outlook.Application")
Set olNs = ObjO.GetNamespace("MAPI")
Set objFolder = olNs.GetDefaultFolder(6)

For Each item1 In objFolder.Items

Dim sa, bc
bc = item1.ReceivedTime
sa = Format(item1.ReceivedTime, "dd-MM-yyyy")

spa = "27/02/2018"

If item1.UnRead And item1.SenderEmailAddress = "harshahowrang@gmail.com" And sa < spa Then

我可以使用 Item1 对象获取日期和时间,但对我来说具有挑战性的部分是获取从今天起过去 30 天的信息。

不应该超过 1 个月.. 所以每个月我都需要生成这个宏来给出特定用户的电子邮件的日期和时间,并将该信息保存在 excel 表中。

这是我每个月手动进行的每月活动

【问题讨论】:

标签: excel vba outlook excel-2010 outlook-filter


【解决方案1】:

一种低效的方法,不首先按适用的时间段限制项目。

Private Sub findByDate()

Dim ObjO As Object
Dim olNs As Object
Dim objFolder As Object

Dim item1 As Object

Dim sa As Date
Dim spa As Date

Set ObjO = CreateObject("Outlook.Application")
Set olNs = ObjO.GetNamespace("MAPI")
Set objFolder = olNs.GetDefaultFolder(6)

Set objFolder = olNs.GetDefaultFolder(6)
Debug.Print objFolder

spa = "27/02/2018"
Debug.Print "Oldest date.....: " & spa - 30

Debug.Print "Most recent date: " & spa

For Each item1 In objFolder.Items

    sa = Format(item1.ReceivedTime, "dd-MM-yyyy")

    If sa <= spa Then
        If sa > spa - 30 Then

            Debug.Print item1.ReceivedTime & " - " & item1.Subject

        End If
    End If

Next

Set ObjO = Nothing
Set olNs = Nothing
Set objFolder = Nothing

End Sub

【讨论】:

  • 这工作完美..根据我的要求进行了一些更改,现在可以工作了。谢谢尼顿.. :)
  • 我在我的电脑上完美工作,没有任何问题和错误。但是当我尝试在其他电脑上运行时,它给出了一个错误。运行时错误 430。类不支持自动化或不支持预期的接口。请帮帮我
  • 如果您有新问题,请单独提出。包括代码和错误行等信息,以增加答案的概率。
【解决方案2】:

目前您的变量 saspa 被视为字符串。您必须将它们声明为日期才能进行日期比较。

dim sa as date
dim bc as date
dim spa as date

【讨论】:

    【解决方案3】:

    以 niton 为例 这里是另一个例子 过滤 30 天

    Dim DateDiff As Long
    DateDiff = Now - 30
    Dim Filter As String
    Filter = "[SentOn]  < '" & Month(DateDiff) & "/" & _
                               Day(DateDiff) & "/" & _
                               Year(DateDiff) & "'"
    

    使用Items.Restrict Method (Outlook)

    循环示例https://stackoverflow.com/a/42547062/4539709

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2019-12-31
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      相关资源
      最近更新 更多