Outlook - List Attachments in Message Body

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,845
Office Version
  1. 2010
Platform
  1. 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:

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
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I saw your post on the other forum, reread it a couple of times too, and to be honest I'm not sure what you are asking.

Do you want to split up the list of attachment so each separate part, eg forwards, replies etc, has its own list?
 
Upvote 0
Dang, sorry to have been unclear.

When I push the button now, it lists the email's attachments at the bottom of the email, like this:

Attachments:
1. SomeExcelFile.xls
2. SomeBigProposal.pdf
3. SomeOtherAttachment.ext
4. ...

What I would prefer is that it puts the list at the cursor position, which I would typically position at the end of the top message in the stack.
 
Upvote 0
The HTML doesn't render properly in the first post; it does at the cross-post.
 
Upvote 0
I'm no expert with Outlook VBA either, can use it to send emails, create appointments etc, but I had a quick look at this.

The only things I could find regarding the cursor referenced using Word as the email editor or using SendKeys, not sure if either would be appropriate.

I also looked into splitting the HTMLBody hoping it would be possible to separate out the 'parts' and then 'stitch' them back together, including the attachment list.

Unfortunately, as far as I could see anyway, it consists of one big block of HTML code with no discernible delimiters.

There's probably stuff out there but like I said I only took a quick look.:)
 
Upvote 0
Thanks for looking, Norie.

To edit a message from the UI, you need to press the "Edit Message" action button; the code doesn't need to do that to edit the message, so there may not be anything equivalent in the object model to enable that, short of using SendKeys as you describe -- but I really don't want to do that.

Anyway, thank you again.
 
Upvote 0
I found https://msdn.microsoft.com/en-us/library/dd492012(v=office.12).aspx, which is obsolete but interesting reading. In particular,

The Outlook object model itself provides no direct way to determine the position of the cursor in an item body. However, since the editor for every item body (except on “sticky notes” and distribution lists) is a special version of Microsoft Word, you can use Word techniques not only to add text at the insertion point, but also to add formatted text anywhere in the item, or even to add a picture.

That jibes with your comment about using Word.

I think I'll just live with my macro as is.
 
Upvote 0
shg

Just had a thought, it's possible to change the way original messages appear in a reply.

For example you can have the reply appear indented with a line between it and the original message.

Perhaps some scope there for breaking things up.

Just an idea though.:)
 
Upvote 0

Forum statistics

Threads
1,223,794
Messages
6,174,643
Members
452,575
Latest member
Fstick546

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top