我有一些脚本根据一些传入的电子邮件规则附加和发送文件,这些规则非常有效。
Sub AddAttachment(Item As Outlook.MailItem)
Dim myOlApp As Outlook.Application
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.CreateItemFromTemplate("C:\Templates\test upload file.oft")
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\TEST\150520ABCDE.txt"
myItem.Send
End Sub
我遇到的麻烦是附件文件名“150520ABCDE.txt”每天都在更改。 “150520”是约会。
因此我有一些脚本应该根据日期分配正确的文件名。
Function LPad (str, pad, length)
LPad = String(length - Len(str), pad) & str
End Function
y = Year(Now)
m = Month(Now)
d = Day(Now)
f = "C:\Test\" & Mid(y, 3, 2) & LPad(m, "0", 2) & LPad(d, "0", 2) & "ABCDE.txt"
myAttachments.Add f
我的问题是如何将它们连接在一起?
答案 0 :(得分:0)
我实际上会做的是:
Sub AddAttachment(Item As Outlook.MailItem)
Dim myOlApp As Outlook.Application
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.CreateItemFromTemplate("C:\Templates\test upload file.oft")
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\TEST\" & Format(Date, "yymmdd") & "ABCDE.txt"
myItem.Send
End Sub
希望这有帮助
答案 1 :(得分:0)
我注意到脚本中的以下代码行:
设置myOlApp = CreateObject(“Outlook.Application”)
为什么需要在代码中创建新的Outlook Application实例?相反,您可以在VBA中使用Application属性,或者只使用传递的参数来检索所需的属性。
Sub AddAttachment(Item As Outlook.MailItem)
Dim myOlApp As Outlook.Application
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.CreateItemFromTemplate("C:\Templates\test upload file.oft")
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\Test\" & Mid(Year(Now), 3, 2) & LPad(Month(Now), "0", 2) & LPad(Day(Now), "0", 2) & "ABCDE.txt"
' or simply use the Format method
' myAttachments.Add "C:\TEST\" & Format(Date, "yymmdd") & "ABCDE.txt"
myItem.Send
End Sub
Function LPad (str, pad, length)
LPad = String(length - Len(str), pad) & str
End Function
最后,您可能会发现Getting Started with VBA in Outlook 2010文章很有帮助。