如果收到Outlook主题和日期 [英] If Outlook Subject and Date Received
问题描述
我正在使用来自excel的vba在我的默认Outlook收件箱中搜索主题行上包含每周运营报告"的电子邮件,然后转移到excel.如何修改我的代码以符合条件:如果主题行显示"Weekly Op's Report for",并且"Received date =今天",则复制电子邮件正文.这是我的代码如下:
I am using vba from excel to search my default outlook inbox for emails that contain "Weekly Op's Report for" on the subject line and transfer to excel. How can I modify my code to meet to conditions: if subject line reads "Weekly Op's Report for" and Received date = today then copy email body. Here is my code below:
Sub GetFromInbox()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim olItms As Outlook.Items
Dim olMail As Variant
Dim i As Long
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = olFldr.Items
olItms.Sort "Subject"
i = 1
For Each olMail In olItms
If InStr(1, olMail.Subject, "Weekly Op's Report for ") > 0 Then
ThisWorkbook.Sheets("tester").Cells(i, 1).Value = olMail.Body
i = i + 1
End If
Next olMail
Set olFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
推荐答案
您可以使用olMail对象的ReceivedTime属性来实现您的请求.请尝试以下代码:
You can use ReceivedTime property of olMail object to achieve your request. Please try this code below:
Sub GetFromInbox()
Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim olFldr As Outlook.MAPIFolder
Dim olItms As Outlook.Items
Dim olMail As Variant
Dim i As Long
Dim date1 As String
Dim date2 As String
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = olFldr.Items
date1 = Date
olItms.Sort "Subject"
olItms.Sort "[ReceivedTime]", True
i = 1
For Each olMail In olItms
date2 = olMail.ReceivedTime
If InStr(1, olMail.Subject, "Weekly Op's Report for ") > 0 And DateDiff("d", date1, date2) = 0 Then
ThisWorkbook.Sheets("tester").Cells(i, 1).Value = olMail.Body
i = i + 1
End If
Next olMail
Set olFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
有关DateDiff函数的更多信息,请参见如何使用DATEDIFF函数(VBA).
For more DateDiff function, please see How to use the DATEDIFF Function (VBA).
这篇关于如果收到Outlook主题和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!