将主题行中的文本与当前日期进行比较-1 [英] Compare text from the subject line against current date - 1
问题描述
我有每隔几天寄出的邮件列表,主题为:
I've a list of mails that come in every few days with the subject as:
- 客户订单2015年1月4日
- 客户订单2015年2月4日
- 客户订单2015年3月4日
- 客户订单2015年1月3日
- 客户订单2015年2月3日
- 客户订单2015年3月3日
仅当主题为"CUSTOMER ORDERS1"和"04/11/2015 =今天的日期-1"时,我才想做某事.
I want to do something only when subject is CUSTOMER ORDERS1 and 04/11/2015 = todays date - 1.
我认为我需要
- 从主题中提取文本日期,然后将其转换为DD/MM/YYYY的日期格式,然后将该日期与该格式的当前-1比较.
- 我还需要从主题中提取CUSTOMER ORDERS1,并将其与"CUSTOMER ORDERS1"进行比较.
下面是代码
Public Sub saveAttachtoDisk()
Dim olApp As Outlook.Application, _
oNS As Outlook.NameSpace, _
oFld As Outlook.Folder, _
oMails As Outlook.Items, _
oMail As Outlook.MailItem, _
oAtt As Outlook.Attachment, _
SaveFolder As String, _
Yesterday as String
SaveFolder = "d:\temp\"
Yesterday = Format(Now()-1, "mm.dd.yy")
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set oNS = olApp.GetNamespace("MAPI")
Set oFld = oNS.GetDefaultFolder(olFolderInbox)
Set oMails = oFld.Items
For Each oMail In oMails
If InStr(1, oMail.Subject, yesterday)
and InStr(1, oMail.Subject, 'CUSTOMER ORDERS1') Then
'----Your code comes here
For Each oAtt In oMail.Attachments
oAtt.SaveAsFile SaveFolder & "\" & oAtt.DisplayName
Set oAtt = Nothing
Next oAtt
Else
End If
Next oMail
End Sub
推荐答案
您可以通过将"Txt_to_Find"
替换为"CUSTOMER ORDERS1-" & format(date-1,"dd/mm/yyyy")
但是,如果您要查找整个文本字符串,则instr
效率低下,那么最好只执行oMail.Subject = "CUSTOMER ORDERS1-" & format(date-1,"dd/mm/yyyy")
But if you are looking for the entire text string, then instr
is inefficient, you would be better just doing oMail.Subject = "CUSTOMER ORDERS1-" & format(date-1,"dd/mm/yyyy")
不过,请注意使用用法,instr
返回一个字符串在另一个字符串中的位置,而不是是否存在字符串的简单真/假,因此,如果instr() > 0
需要获取TRUE
,则需要执行instr() > 0
字符串确实存在.
Just a note on usage though, instr
returns the position of a string within another string, rather than a simple true/false of whether it exists, so you need to do instr() > 0
to get TRUE
if the string does exist.
希望这会有所帮助!
这篇关于将主题行中的文本与当前日期进行比较-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!