从VBA搜索Outlook电子邮件 [英] Search Outlook Emails from VBA
问题描述
给定的代码成功运行。它在Outlook已发送邮件文件夹中搜索电子邮件主题。搜索基于特定时间段内的特定日期进行。例如,下面的代码查找2018年7月20日12:00 AM至11:59 PM之间发送的电子邮件标题``星期五发送的电子邮件''。
The given code works successfully. It searches for an email subject in outlook Sent Items folder. The search happens based on a specific date within specific time period. For example, the code below looks for the email title "Test Email Sent on Friday" that was sent on July 20, 2018 between 12:00 AM and 11:59 PM.
除了现有的搜索条件外,如何过滤发送给特定用户的电子邮件。我要检查[收件人]字段。如果[收件人]有收件人x @ email.com,y @ email.com或z@email.com,则不要返回搜索结果。如果[收件人]部分没有以下电子邮件中的任何一个,则搜索应返回是。已找到电子邮件:x @ email.com,y @ email.com或z@email.com。
In addition to my existing search criteria, how can I filter out emails that were sent out to specific users. I want to check [To] field. If [To] had recipients x@email.com, y@email.com, or z@email.com, then do not return the search results. The search should return "Yes. Email found" if [To] section doesn't have either of these emails: x@email.com, y@email.com, or z@email.com.
Public Function is_email_sent()
Dim olApp As Object
Dim olNs As Object
Dim olFldr As Object
Dim olItms As Object
Dim objItem As Object
On Error Resume Next
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.Folders("myemail@example.com").Folders("Sent Items")
Set olItms = olFldr.Items
Set objItem = olItms.Restrict("[Subject] = ""Test Email Sent on Friday"" And [SentOn] >= ""7/20/2018 12:00 AM"" AND [SentOn] <= ""7/20/2018 11:59 PM""")
If objItem.Count = 0 Then
MsgBox "No. Email not found"
Else
MsgBox "Yes. Email found"
End If
Set olApp = Nothing
Set olNs = Nothing
Set olFldr = Nothing
Set olItms = Nothing
Set objItem = Nothing
End Function
推荐答案
这是解决方案
Public Function is_email_sent()
Dim olApp As Object
Dim olNs As Object
Dim olFldr As Object
Dim olItms As Object
Dim objItem As Object
On Error Resume Next
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.Folders("myemail@example.com").Folders("Sent Items")
Set olItms = olFldr.Items
Set objItem = olItms.Restrict("[Subject] = ""Test Email Sent on Friday"" And [SentOn] >= ""7/20/2018 12:00 AM"" AND [SentOn] <= ""7/20/2018 11:59 PM""")
If objItem.Count = 0 Then
is_email_sent_out_to_business = False
Else '*** Solution
Dim o As Object
For Each o In objItem
If Not (InStr(o.To, "x@email.com") > 0 Or InStr(o.To, "y@email.com") > 0) Then
MsgBox "Yes. Email found"
Exit For
Else
MsgBox "No. Email not found"
End If
Next
End If
Set olApp = Nothing
Set olNs = Nothing
Set olFldr = Nothing
Set olItms = Nothing
Set objItem = Nothing
End Function
这篇关于从VBA搜索Outlook电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!