将主题行中的文本与当前日期进行比较 - 1

时间:2015-11-05 00:34:59

标签: vba outlook outlook-vba

我列出了每隔几天发出的邮件列表:

  • CUSTOMER ORDERS1-04 / 11/2015
  • CUSTOMER ORDERS2-04 / 11/2015
  • CUSTOMER ORDERS3-04 / 11/2015
  • CUSTOMER ORDERS1-03 / 11/2015
  • CUSTOMER ORDERS2-03 / 11/2015
  • CUSTOMER ORDERS3-03 / 11/2015

我想做的事情只有当主题是CUSTOMER ORDERS1和04/11/2015 =今天的日期 - 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

1 个答案:

答案 0 :(得分:1)

您可以将"Txt_to_Find"替换为"CUSTOMER ORDERS1-" & format(date-1,"dd/mm/yyyy")

来进行整个比较

但如果您要查找整个文本字符串,那么instr效率低下,您最好只做oMail.Subject = "CUSTOMER ORDERS1-" & format(date-1,"dd/mm/yyyy")

只是关于用法的说明,instr返回另一个字符串中字符串的位置,而不是它是否存在的简单true / false,因此您需要instr() > 0来获取{ {1}}如果字符串确实存在。

希望这有帮助!