我需要搜索邮件在发送的项目,发送在当前日期和主题为“任务完成”。有时,主题可能有额外的文本,如在2017年7月完成的任务或任务完成的01/09/2017。
我找到了这个Outlook代码,它显示找到的邮件。我希望使用通配符搜索选项在Excel中运行代码,并打开Excel文件。
我尝试用通配符"*“搜索主题,比如"Task *”和"Task on &Format(日期,"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。
发布于 2017-01-10 09:25:15
为了循环遍历已发送项目文件夹中的所有项,包括您可能拥有的日历事件,请使用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 Subhttps://stackoverflow.com/questions/41564816
复制相似问题