用于发送带附件的电子邮件的Outlook VBA

用于发送带附件的电子邮件的Outlook VBA

问题描述:

我需要在一天内将近100封电子邮件发送给具有不同文件名的不同人。下面的代码工作得很好,但问题是,我必须附上的文件应该是过去一天的日期。例如,今天的日期是2013年3月7日(7-03-13)。我收到了RN2425 06-03-13.xls的文件,这些文件每天都在变化。我想查找特定目录D:\ Reporting \ Daily \ RN2425 \ RN2425(一天前的日期戳记).xls用于发送带附件的电子邮件的Outlook VBA

以前的文件请使用此代码来帮助我我需要更改文件名中的日期。我希望这是自动完成的。

Sub CreateEmail(Subject As String, Body As String, ToSend As String, CCs As String, FilePathtoAdd As String) 

'write the default Outlook contact name list to the active worksheet 

Dim OlApp As Object 
Dim OlMail As MailItem 
Dim ToRecipient As Variant 
Dim CcRecipient As Variant 


'Set OlApp = CreateObject("Outlook.Application") 
'Set OlMail = OlApp.CreateItem(olMailItem) 

Set OlApp = Application 
Set OlMail = OlApp.CreateItem(olMailItem) 

'For Each ToRecipient In Array("[email protected]", "[email protected]", "[email protected]") 
'OlMail.Recipients.Add ToRecipient 
OlMail.Recipients.Add ToSend 
'Next ToRecipient 



'For Each CcRecipient In Array("[email protected]", "[email protected]", "[email protected]") 
'With OlMail.Recipients.Add(CcRecipient) 
'.Type = 2 
'End With 
'Next CcRecipient 
Dim Temp As Recipient 
Set Temp = OlMail.Recipients.Add(CCs) 
Temp.Type = olCC 


'fill in Subject field 
OlMail.Subject = Subject 
OlMail.Body = Body 

'Add the active workbook as an attachment 
' OlMail.Attachments.Add "C:\Users\Ali\Desktop\Sentence Correction\Comparisons.pdf" 
If FilePathtoAdd <> "" Then 
    OlMail.Attachments.Add FilePathtoAdd 
End If 
'Display the message 
OlMail.Display 'change this to OlMail.Send if you just want to send it without previewing it 

End Sub 
Sub EmailIt() 
CreateEmail "This is Subject", "Body", "[email protected], [email protected]", "[email protected], [email protected]", "E:\Ali's Documents\RN2425 06-03-13.xls" 
CreateEmail "This is Subject", "Body", "[email protected], [email protected]", "[email protected], [email protected]", "E:\Ali's Documents\RN2425 06-03-13.xls" 
CreateEmail "This is Subject", "Body", "[email protected], [email protected]", "[email protected], [email protected]", "E:\Ali's Documents\RN2425 06-03-13.xls" 
CreateEmail "This is Subject", "Body", "[email protected], [email protected]", "[email protected], [email protected]", "E:\Ali's Documents\RN2425 06-03-13.xls" 
CreateEmail "This is Subject", "Body", "[email protected], [email protected]", "[email protected], [email protected]", "E:\Ali's Documents\RN2425 06-03-13.xls" 
CreateEmail "This is Subject", "Body", "[email protected], [email protected]", "[email protected], [email protected]", "E:\Ali's Documents\AVSEQ03 Comp 1.avi" 

End Sub 

为了得到今天的日期正确的格式:

  • 格式(Date, “DD-MM-YY”)

要获得昨天的日期:

  • DateAdd(“d”,-1,Date)

全部放在一起:

  • “E:\阿里的文档\ RN2425” &格式(DATEADD( “d”,-1,日期), “DD-MM-YY”)& “的.xls”