VBA从Excel中search已发送邮件中的Outlook 2010邮件

我需要一个Excel VBAsearch当前date发送的已发邮件中的Outlook 2010邮件,并将其作为“任务已完成”(有时候,主题可能会有一个额外的文本,如任务在07/01/2017完成或任务已完成01/09 / 2017年); 如果find,然后从指定的path打开一个Excel文件。

我在Googlesearch中发现了这个Outlook VBA代码,它显示了search到的邮件,但是我希望代码能够在通配符search选项中运行,并打开excel。

我尝试使用通配符“ * ”search主题,例如“语法错误/编译错误”中的“任务完成* ”和“任务已完成并格式化(date,”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 )。

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

我已经添加了两行代码,将所有匹配的电子邮件输出到列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