Public Sub SaveAttach(Item As Outlook.MailItem)
'Declare variables
Dim folderPath As String
folderPath = "E:\attachments\"
Dim condition As String
condition = "*"
Dim olAtt As Attachment
Dim i As Integer
'Go through each attachments
If Item.Attachments.Count > 0 Then
For i = 1 To Item.Attachments.Count
Set olAtt = Item.Attachments(i)
'Save the attachment if it matches the condition
If olAtt.FileName Like condition Then
olAtt.SaveAsFile folderPath & DateTime.Timer & "_" & olAtt.FileName
End If
Next
End If
Set olAtt = Nothing
End Sub
测试结果:
1. 若VB在保存时没有特别加用以区分文件名的随机函数(日期,tmp类似)
则OUTLOOK在自动保存文件附件时会以override方式保存;
2. 若保存时加了些区分名字,那假设一封邮件中含有相同文件名的附件
在自动保存时不会override;
3. 同时可以先判断路径内是否已有此文件,若无则自动保存,否则不保存;
如下为先判断再保存:
1 Public Sub SaveAttach(Item As Outlook.MailItem)
2 'Declare variables
3 Dim folderPath As String
4 folderPath = "E:\attachments\"
5 Dim condition As String
6 condition = "*"
7
8 Dim dateformat
9 dateformat = Format(Now, "yyyy-mm-dd H-mm")
10 Dim fullpath As String
11
12 Dim olAtt As Attachment
13 Dim i As Integer
14 'Go through each attachments
15 If Item.Attachments.Count > 0 Then
16 For i = 1 To Item.Attachments.Count
17 Set olAtt = Item.Attachments(i)
18 'Save the attachment if it matches the condition
19 If olAtt.FileName Like condition Then
20 fullpath = folderPath & "\" & olAtt.FileName
21 If Dir(fullpath) = "" Then
22 olAtt.SaveAsFile folderPath & olAtt.FileName
23 End If
24 'Next
25 End If
26 Next
27 End If
28
29 Set olAtt = Nothing
30 End Sub