Roy*_*ell 37 outlook vba attachment outlook-vba
我正在尝试在Outlook中获取一个VBA宏,它将保存电子邮件对特定文件夹的附件,并将收到的日期添加到文件名中.
我的谷歌搜索让我这么远:
Public Sub saveAttachtoDisk (itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat As String
saveFolder = "C:\Temp\"
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
第一个显而易见的事情是它将当前时间应用于文件名而不是接收时间,但我似乎无法改变它.我的理论是Outlook.Attachment没有ReceivedTime
,并且必须引用电子邮件本身.
其次,这似乎根本不起作用,哈!它开始修补的第一天工作,但之后它停止保存文件.
小智 35
这是我的保存附件脚本.您选择了要保存附件的所有邮件,并在那里保存副本.它还会在消息正文中添加文本,指示附件的保存位置.您可以轻松更改文件夹名称以包含日期,但在开始保存文件之前,您需要确保该文件夹已存在.
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
' Get the path to your My Documents folder
strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
On Error Resume Next
' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection
' Set the Attachment folder.
strFolderpath = strFolderpath & "\Attachments\"
' Check each selected item for attachments. If attachments exist,
' save them to the strFolderPath folder and strip them from the item.
For Each objMsg In objSelection
' This code only strips attachments from mail items.
' If objMsg.class=olMail Then
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
strDeletedFiles = ""
If lngCount > 0 Then
' We need to use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For i = lngCount To 1 Step -1
' Save attachment before deleting from item.
' Get the file name.
strFile = objAttachments.Item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile
' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
' Delete the attachment.
objAttachments.Item(i).Delete
'write the save as path to a string to add to the message
'check for html and use html tags in link
If objMsg.BodyFormat <> olFormatHTML Then
strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
Else
strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
strFile & "'>" & strFile & "</a>"
End If
'Use the MsgBox command to troubleshoot. Remove it from the final code.
'MsgBox strDeletedFiles
Next i
' Adds the filename string to the message body and save it
' Check for HTML body
If objMsg.BodyFormat <> olFormatHTML Then
objMsg.Body = vbCrLf & "The file(s) were saved to " & strDeletedFiles & vbCrLf & objMsg.Body
Else
objMsg.HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & objMsg.HTMLBody
End If
objMsg.Save
End If
Next
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
Run Code Online (Sandbox Code Playgroud)
见ReceivedTime
财产
http://msdn.microsoft.com/en-us/library/office/aa171873(v=office.11).aspx
您在SaveAs文件行\
的末尾添加了另一个.可能是个问题.在添加路径分隔符之前先进行测试.C:\Temp\
dateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd H-mm")
saveFolder = "C:\Temp"
Run Code Online (Sandbox Code Playgroud)
你没有这样设置,objAtt
所以不需要" Set objAtt = Nothing
".如果有的话就在End Sub
不在循环之前.
Public Sub saveAttachtoDisk (itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String Dim dateFormat
dateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd H-mm") saveFolder = "C:\Temp"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
回复:它开始修补的第一天工作,但之后它停止保存文件.
这通常是由安全设置引起的.这是一个"陷阱",为初次使用者设置允许宏然后将其带走. http://www.slipstick.com/outlook-developer/how-to-use-outlooks-vba-editor/