Jhecht
Jhecht

Reputation: 4435

Retrieving Outlook Attachments in VBA from sent mail

What I am trying to get done is to have it so that on a few E-Mail templates we have at my job, when we drag and drop a certain Excel file to it, it will select and copy a range of that Excel file into the body of the email. My only question, as I've looked everywhere and all the code I've found seems to be for emails that are being received, not written, is how do I access:

  1. The attachments in an email that I am writing
  2. The body of the E-Mail I am writing

I'm sure I can get the code for everything else I want once I can get the attachments for the current Email.

Before any mentions "just record the macro and see how outlook does it" for some reason my outlook does not have a "record macro" item anywhere, as that was my first go-to as well.

Upvotes: 1

Views: 689

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149325

how do I access: The attachments in an email that I am writing The body of the E-Mail I am writing

Here is a very basic example. I have not done any error handling but I am sure you can take care of it.

Let's say the email that you are writing looks like this

enter image description here

All you need is this code

Sub Sample()
    Dim NewMail As MailItem, oInspector As Inspector
    Set oInspector = Application.ActiveInspector

    '~~> Get the current open item
    Set NewMail = oInspector.CurrentItem

    With NewMail
        Debug.Print .To
        Debug.Print .subject
        Debug.Print .Body

        AttchCount = .Attachments.Count

        If AttchCount > 0 Then
            For I = 1 To AttchCount
                '~~> Print Attachment names
                Debug.Print .Attachments.Item(I).DisplayName
            Next I
        End If
    End With
End Sub

Output

enter image description here

Upvotes: 1

Related Questions