我列出了每隔几天发出的邮件列表:
我想做的事情只有当主题是CUSTOMER ORDERS1和04/11/2015 =今天的日期 - 1.
我想我需要
以下是代码
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
答案 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}}如果字符串确实存在。
希望这有帮助!