excel - 从 Excel 中按日期和带有通配符的主题搜索已发送邮件中的邮件

标签 excel vba outlook

我需要在已发送邮件中搜索邮件,该邮件是在当前日期发送的,主题为“任务已完成”。有时,主题可能会有其他文本,例如 Task Completed on 07/01/2017 或 Task Completed 01/09/2017。

我找到了这个 Outlook VBA 代码,它显示找到的邮件。我希望代码使用通配符搜索选项在 Excel 中运行并打开一个 Excel 文件。

我尝试使用通配符“*”搜索主题,例如“任务完成 *”和“任务完成日期和格式(日期,“dd/mm/yyyy”)”,但出现语法错误/编译错误

Sub Test()

Dim olApp As Outlook.Application
Dim olNs As NameSpace
Dim Fldr As MAPIFolder
Dim olMail As Outlook.MailItem
Dim i As Integer

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderSentMail)
i = 1

For Each olMail In Fldr.Items
    If InStr(olMail.Subject, "Task Completed on 07/01/2017") <> 0 Then
        olMail.Display
        i = i + 1    
    End If    
Next olMail

End Sub

我正在使用 Office 2010。

最佳答案

为了遍历 中的所有项目已发送元素文件夹,包括您可能拥有的日历事件,请使用 Dim olMail As Object (而不是 AS Outlook.MailItem )。

要在电子邮件标题中的某处查找“任务已完成”字符串,请使用 If olMail.Subject Like "*Task Completed*" Then (在搜索字符串前后添加通配符 *)。

我添加了 2 行代码,将所有匹配的电子邮件输出到 A 列和 B 列的工作表中。

代码

Option Explicit

Sub Test()

Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Object
Dim i As Integer, j As Integer

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderSentMail)

i = 1
For Each olMail In Fldr.Items
    ' check if mail subject contains "Task Completed" in the email title
    If olMail.Subject Like "*Task Completed*" Then
        'Range("A" & i).Value = olMail.Subject ' <-- output email name to column A
        'Range("B" & i).Value = olMail.SentOn ' <-- output email sent date to column B
        olMail.Display ' show email through Excel
        i = i + 1
    End If
Next olMail

End Sub

关于excel - 从 Excel 中按日期和带有通配符的主题搜索已发送邮件中的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41564816/

相关文章:

powershell - 使用 PowerShell 从 outlook 读取最近的电子邮件

java - 类的实例导致 nullPointerException

excel - 复制和转置时克服 255 个字符的限制

java - 如何使用java设置Excel工作表的密码

vba - 通过电子邮件发送范围而不是行

c# 从邮件中查找邮件

c# - 共享 Outlook MailItem UserProperties

excel - Excel 中的错误?尽管单元格不为空,但显示空白区域

excel - 使用findnext填充多维数组VBA Excel

excel搜索范围特定文本复制包含文本的单元格