我正在尝试通过Outlook中的规则运行宏,将附件保存到文件夹中。
电子邮件有时会有多个附件。我正在尝试按顺序保存文件,因此,例如,如果我关闭电子邮件,我可以轻松地看到与其对应的文件。
我在网上找到以下内容:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\PathToDirectory\"
Dim dateFormat As String
dateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd Hmm ")
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Next
End Sub
我试图与objatt.displayname
一起玩,但没有运气。我已经尝试分配新名称并创建一个新的循环来命名文件1,文件2等文件,但是当我这样做时,我丢失了文件扩展名。
更新版本:
Option Explicit
Public Sub save_attachments(itm As Outlook.MailItem)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim strExt As String
Dim i As Long
Dim savefolder As String
i = 1
savefolder = "C:\Users\w\desktop\test"
For Each objAtt In itm.Attachments
i = i + 1
strExt = fso.GetExtensionName(objAtt.DisplayName)
objAtt.SaveAsFile savefolder & "\" & dateFormat & " - File " & i & "." & strExt
Next
End Sub
答案 0 :(得分:2)
你可以在现有的子程序中做这样的事情。这会增加"File"
个数字并仍保留扩展名。
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim strExt As String
Dim i As Long
For Each objAtt In itm.Attachments
i = i + 1
strExt = fso.GetExtensionName(objAtt.DisplayName)
objAtt.SaveAsFile saveFolder & "\" & dateFormat & " - File " & i & "." & strExt
Next
答案 1 :(得分:0)
我一直在使用类似的解决方案。我最近添加了这种编码的安静,以避免保存嵌入的图像:
Extension = LCase$(Right$(FileNm, 3))
If Extension = "png" Or Extension = "gif" Or Extension = "jpg" Then
*** Save File ***
Endif
当然假设您没有收到要保存的图像文件。
如果你在For Next循环之前放置它,你可以选择多个电子邮件:
For Each Item In Application.ActiveExplorer.Selection
当然,你必须把你的i = i + 1放在其他地方。
您还可以在电子邮件中添加指向您文件的链接:
FileNameb = Replace(filename, " ", "%20")
Link = "<a href=" + FileNameb + ">" + filename + "</a><br />"
Item.HTMLBody = Item.HTMLBody + Link
这会从邮件中删除附件并保存:
For i = 1 To Item.Attachments.Count
Item.Attachments.Remove 1: 'Remove all attachments
Next i
Item.UnRead = False: 'Mark e mail as read
Item.Save
玩得开心!