问题
I have code to export Outlook attachments to a local folder.
I noticed the little images in the signature are also saved as attachments.
I think excluding the signature images could be done with an If then along the lines of:
For i = lngCount To 1 Step -1
If objAttachments.Item(i).Size > 6000 Then
I don't know where to place in my code, especially the End If (after or before the Next).
Here is my code:
Public Sub Renamefileandexcludesignature(Item As Outlook.MailItem)
Dim Atmt As Outlook.Attachment
Dim SavePath As String
Dim FileName As String
SavePath = "C:\Users\Antoine\Documents"
FileName = "Antoine" & ".csv"
For Each Atmt In Item.Attachments
Atmt.SaveAsFile SavePath & "\" & FileName
Next
Set Atmt = Nothing
End Sub
回答1:
If you're after downloading a specific file type you could check the extension of the attachment (untested, but should work):
Public Sub Renamefileandexcludesignature(Item As Outlook.MailItem)
Dim Atmt As Outlook.Attachment
Dim SavePath As String
Dim FileName As String
Dim objFSO As Object
Dim sExt As String
SavePath = "C:\Users\Antoine\Documents"
FileName = "Antoine" & ".csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each Atmt In Item.Attachments
sExt = objFSO.GetExtensionName(Atmt.FileName)
Select Case sExt
Case "jpg", "png"
'Do nothing
Case Else
Atmt.SaveAsFile SavePath & "\" & FileName
End Select
Next
Set Atmt = Nothing
End Sub
But I have little to no knowledge in VBA and don't know where to place in my code, especially the EndIf ( after or before the Next)
These have to go in order -
If you use IF and then FOR you have to use NEXT first to close the FOR and then END IF to close the IF.
If you use FOR and then IF you have to use END IF to close the IF before you can use NEXT to close the FOR.
Hope that made sense.
回答2:
To use For i = lngCount To 1 Step -1 (Looping through macro backwards)
Declare your variables
Dim objAttachments As Outlook.Attachments
Dim i As Long
Dim lngCount As Long
Now run your loop to save Object item
If Item.Attachments.Count > 0 Then
For i = lngCount To 1 Step -1
If objAttachments.Item(i).Size > 6000 Then
objAttachments.Item(i).SaveAsFile FileName
End If
Next i
End If
来源:https://stackoverflow.com/questions/37433626/exclude-signature-images-when-exporting-attachments-to-a-folder