【问题标题】:Save Outlook attachment to disk with size filter使用大小过滤器将 Outlook 附件保存到磁盘
【发布时间】:2021-11-06 01:26:45
【问题描述】:

参考这里Save Outlook attachment to disk的惊人脚本

我想按大小过滤附件。我现在使用该脚本有一段时间了,但该脚本还保存了公司徽标等。这会提供大量 1kb 文件并更改邮件布局。

我希望脚本忽略小于 10kb 的文件。有没有人可以帮我在下面的脚本中实现这个;

Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim pobjMsg As Outlook.MailItem 'Object
Dim objSelection As Outlook.Selection

' 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
 
For Each pobjMsg In objSelection
SaveAttachments_Parameter pobjMsg
Next
 
ExitSub:
 
Set pobjMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
 
Public Sub SaveAttachments_Parameter(objMsg As MailItem)
Dim objAttachments As Outlook.Attachments
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
 
' Set the Attachment folder.
strFolderpath = "\\path\"
 
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
 
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
'=======================================================

tempstr = strFile 'strtoclean
charArray = Array("?", "/", "\", ":", "*", """", "<", ">", ",", "&", "#", "~", "%", "{", "}", "+", "_")

For Each tmpChar In charArray

Select Case tmpChar
Case "&"
changeTo = " and "
Case ":"
changeTo = "-"
Case Else
changeTo = " "
End Select

tempstr = Replace(tempstr, tmpChar, changeTo)
Next

strFile = tempstr



'==========================================================


' Combine with the path to the Temp folder.
strFile = strFolderpath & Format(objMsg.ReceivedTime, "yyyy-MM-dd h-mm-ss") & "." & 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
     
Next i
End If
 
' Adds the filename string to the message body and save it
' Check for HTML body
If objMsg.BodyFormat <> olFormatHTML Then
objMsg.Body = objMsg.Body & vbCrLf & _
"The file(s) were saved to " & strDeletedFiles
Else
objMsg.HTMLBody = objMsg.HTMLBody & "" & _
"The file(s) were saved to " & strDeletedFiles & ""
End If
objMsg.Save
ExitSub:
 
Set objAttachments = Nothing
Set objMsg = Nothing
Set objOL = Nothing
End Sub



Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

End Sub

【问题讨论】:

标签: vba outlook size attachment


【解决方案1】:

Attachment.Size 属性可能会有所帮助。

你可以的

For i = lngCount To 1 Step -1
   if objAttachments.Item(i).Size >= 10240 then
       ...
   end if
Next i

【讨论】:

  • 感谢您的回复。你能详细说明这 3 个点吗?
  • 您已经使用For i = lngCount To 1 Step -1 ... Next i 循环浏览附件。要忽略小于 10kb 的附件,请检查循环内每个附件的大小。所以 3 个点是您当前在该循环中的代码。如果您需要更多帮助,请告诉我。
  • 谢谢,我搞定了
猜你喜欢
  • 2014-12-01
  • 2021-08-18
  • 2011-07-02
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 2013-12-19
相关资源
最近更新 更多