问题
I've a list of mails that come in every few days with the subject as:
- 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
I want to do something only when subject is CUSTOMER ORDERS1 and 04/11/2015 = todays date - 1.
I'm thinking I would need to
- extract textual date from the subject, then convert it to date format as DD/MM/YYYY Then compare that date against the current -1 in that format.
- Also I would need to extract CUSTOMER ORDERS1 from subject and also compare it against "CUSTOMER ORDERS1".
Below is the code
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:
You could do the entire comparison by replacing "Txt_to_Find"
with "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")
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.
Hope this helps!
来源:https://stackoverflow.com/questions/33534706/compare-text-from-the-subject-line-against-current-date-1