我需要在已发送邮件中搜索邮件,该邮件是在当前日期发送的,主题为“任务已完成”。有时,主题可能会有其他文本,例如 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/