2
votes

With the below code I'm getting an error when I try to add an attachment file name to the report. Perhaps it is the syntax?

The error is occurring at Report = Report & currentItem.Attachments.FileName line

The error is "object doesn't support this property or method.

Any ideas?

I am running this code in Outlook,

Private Sub GetAllEmailsInFolder(CurrentFolder As Outlook.Folder, Report As String)
    Dim currentItem
    Dim attachment As attachment
    Dim currentMail As MailItem

    Report = Report & "Folder Name: " & CurrentFolder.Name & " (Store: " & CurrentFolder.Store.DisplayName & ")" & vbCrLf

    For Each currentItem In CurrentFolder.Items
        Report = Report & currentItem.Subject
        Report = Report & vbCrLf
        Report = Report & "----------------------------------------------------------------------------------------"
        Report = Report & vbCrLf
        Report = Report & currentItem.Attachments.FileName

    Next

End Sub

Also, I first run a sub that gets a list of emails:

Public Sub GetListOfEmails()
    'On Error GoTo On_Error

    Dim Session As Outlook.NameSpace
    Dim Report As String
    Dim Folder As Outlook.Folder

    Set Session = Application.Session

    Set Folder = Application.ActiveExplorer.CurrentFolder

    Call GetAllEmailsInFolder(Folder, Report)

    Dim retValue As Boolean
    retValue = CreateReportAsEmail("List of Emails", Report)

Exiting:
        Set Session = Nothing
        Exit Sub
On_Error:
    MsgBox "error=" & Err.Number & " " & Err.Description
    Resume Exiting

End Sub

Then here is the sub I use to create the report in the form of an email I want to copy into excel.

Public Function CreateReportAsEmail(Title As String, Report As String)
    'On Error GoTo On_Error

    Dim Session As Outlook.NameSpace
    Dim mail As MailItem
    Dim MyAddress As AddressEntry
    Dim Inbox As Outlook.Folder

    CreateReportAsEmail = True

    Set Session = Application.Session
    Set Inbox = Session.GetDefaultFolder(olFolderInbox)
    Set mail = Inbox.Items.Add("IPM.Mail")

    Set MyAddress = Session.CurrentUser.AddressEntry
    mail.Recipients.Add (MyAddress.Address)
    mail.Recipients.ResolveAll

    mail.Subject = Title
    mail.Body = Report

    mail.Save
    mail.Display


Exiting:
        Set Session = Nothing
        Exit Function
On_Error:
    CreateReportAsEmail = False
    MsgBox "error=" & Err.Number & " " & Err.Description
    Resume Exiting

End Function
2
The Attachments collection doesn't have a filename property - each individual Attachment doesBigBen
@BigBen oh I see, so perhaps I need to create a function to extract the attachment file name? I'll be using the report for metrics. There are really only a few different kinds of attachments this inbox receives. I want to know they breakdown on a monthly basisMike Mann
You could loop through the Attachments collection and extract the filename from each individual attachment.BigBen
@BigBen anyway you have time to draw that up real quick? I'm not sure how I would go about doing that. ideally the name for each attachment is what I want.Mike Mann

2 Answers

3
votes
  • The Attachments collection does not have a Filename property, but each individual Attachment does. Add an additional loop through the Attachments collection.
  • GetAllEmailsInFolder should be a Function returning a String. A Sub does something; a Function returns something.
  • GetAllEmailsInFolder assumes that all the items within CurrentFolder are MailItems, which might not be the case.
  • Use a different variable name than attachment for each Attachment. Same goes for Folder, Session...

Untested, but something like this:

Private Function GetAllEmailsInFolder(CurrentFolder As Outlook.Folder) As String
    Dim currentItem As Object
    Dim myAttachment As Attachment
    Dim Report as String

    Report = Report & "Folder Name: " & CurrentFolder.Name & " (Store: " & CurrentFolder.Store.DisplayName & ")" & vbCrLf

    For Each currentItem In CurrentFolder.Items
        If TypeOf currentItem Is Outlook.MailItem Then
            Report = Report & currentItem.Subject
            Report = Report & vbCrLf
            Report = Report & "----------------------------------------------------------------------------------------"
            Report = Report & vbCrLf
            For Each myAttachment in currentItem.Attachments
                Report = Report & myAttachment.Filename ' and add formatting inbetween as needed
            Next myAttachment
        End If
    Next currentItem

    GetAllEmailsInFolder = Report
End Sub
1
votes

Here is quick Example, you may need to adjust little on how it displays on email.

    For Each currentItem In CurrentFolder.Items
        Report = Report & currentItem.Subject
        Report = Report & vbCrLf
        Report = Report & "--------------------------------------------------------"
        Report = Report & vbCrLf
'        Report = Report & currentItem.Attachments.FileName

        For Each attachment In currentItem.Attachments
            Debug.Print attachment.FileName
            Report = Report & attachment.FileName
        Next

    Next

MSDN Attachment Object (Outlook)