shg
MrExcel MVP
- Joined
- May 7, 2008
- Messages
- 21,845
- Office Version
- 2010
- Platform
- Windows
I receive many emails with attachments, which I usually remove to my file system, and then delete the attachments from the emails to keep my .ost file from exploding. When I do that, I want to keep a record of the attached filenames so when I'm looking through the emails I can see when they arrived. Despite knowing almost nothing about the Outlook object model, I managed to write the macro below to do just that, and I find it very convenient:
However, there are frequently a stack of forwards and replies in the same mail item. Rather than add the list at the very bottom of all those, I would prefer to put the cursor where I want the list to appear (e.g., just below the message at the top of the stack), and then run the macro. A suggestion to do that would be most welcome.
Also, the macro currently rewrites the whole message just to append the list at the bottom -- that seems clunky. Even without the enhancement above, surely it could just append to the bottom. Or, with the enhancement, insert at the cursor position.
Thanks for reading.
Cross-posted at https://www.excelforum.com/outlook-programming-vba-macros/1179396-list-attachments.html
Code:
Sub ListAttachments()
Dim oAtt As Attachment
Dim asAtt() As String
Dim nAtt As Long
With ActiveInspector.CurrentItem
If .Attachments.Count Then
ReDim asAtt(1 To .Attachments.Count)
For Each oAtt In .Attachments
If LCase(Left(oAtt.FileName, 5)) <> "image" Then
nAtt = nAtt + 1
asAtt(nAtt) = Format(nAtt, "0. ") & oAtt.FileName
End If
Next oAtt
If nAtt Then
ReDim Preserve asAtt(1 To nAtt)
Select Case .BodyFormat
Case olFormatHTML
.HTMLBody = .HTMLBody & "<br> <p class=MsoNormal>" & _
"Attachments:" & "<br>" & _
Join(asAtt, "<br>") & "</p><br>"
Case olFormatPlain, olFormatRichText
.Body = .Body & vbNewLine & _
"Attachments:" & vbNewLine & _
Join(asAtt, vbNewLine) & vbNewLine
End Select
End If
End If
End With
End Sub
However, there are frequently a stack of forwards and replies in the same mail item. Rather than add the list at the very bottom of all those, I would prefer to put the cursor where I want the list to appear (e.g., just below the message at the top of the stack), and then run the macro. A suggestion to do that would be most welcome.
Also, the macro currently rewrites the whole message just to append the list at the bottom -- that seems clunky. Even without the enhancement above, surely it could just append to the bottom. Or, with the enhancement, insert at the cursor position.
Thanks for reading.
Cross-posted at https://www.excelforum.com/outlook-programming-vba-macros/1179396-list-attachments.html