# Outlook - List Attachments in Message Body



## shg (Mar 29, 2017)

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:


```
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


----------



## Norie (Mar 29, 2017)

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?


----------



## shg (Mar 29, 2017)

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.


----------



## shg (Mar 29, 2017)

The HTML doesn't render properly in the first post; it does at the cross-post.


----------



## Norie (Mar 29, 2017)

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.


----------



## shg (Mar 29, 2017)

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.


----------



## shg (Mar 31, 2017)

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.


----------



## Norie (Mar 31, 2017)

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.


----------



## shg (Mar 31, 2017)

I'm good, Norie, thanks.


----------

