Excel VBA - Add Attachment Name To List Of Outlook Email Details

chinchi81

Board Regular
Joined
Jul 13, 2007
Messages
69
Hi all,
I found and adapted the below macro which works perfectly for me.

However, I now need to add in a new column which provides the attachment name(s) on any of the emails and having tried an assortment of variations, I am not getting anywhere in the dimensions and sets to use.

Can anyone out there help?

Thanks
Philip


Sub IHFADMINMAILING()

Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim olItem As Object
Dim olMailItem As Outlook.MailItem
Dim ws As Worksheet
Dim iRow As Long
Dim hdr As Variant

Set ws = ThisWorkbook.Worksheets("IHF ADMIN MAILING")

Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFldr = olNS.Folders("IHF ADMIN MAILING")
Set olFldr = olFldr.Folders("Inbox")

Application.ScreenUpdating = False

ws.Cells.Clear
iRow = 2

With ws
hdr = Array("Sender", "Sender Email Address", "Sender Name", "Subject", "Received Time", "Categories", "Task Completed Date", "Folder", "")
.Range("A1").Resize(, UBound(hdr)) = hdr
End With


For Each olItem In olFldr.Items
If olItem.Class = olMail Then
Set olMailItem = olItem
With olMailItem
ws.Cells(iRow, "A") = .Sender
ws.Cells(iRow, "B") = .SenderEmailAddress
ws.Cells(iRow, "C") = .SenderName
ws.Cells(iRow, "D") = .Subject
ws.Cells(iRow, "E") = .ReceivedTime
ws.Cells(iRow, "F") = .Categories
ws.Cells(iRow, "G") = .TaskCompletedDate
ws.Cells(iRow, "H") = olFldr.Name
iRow = iRow + 1
End With
End If
Next olItem

With ws
.Columns.AutoFit
End With

Application.ScreenUpdating = False

MsgBox "IHF ADMIN MAILING BOX - Analysis Complete!"

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I would check if there is an attachment, if there is then loop them and add their display name to a string... separated by a comma (otherwise you don't know how many columns you will need for them). Something like (changes in red):


Code:
Sub IHFADMINMAILING()
 Dim olApp As Outlook.Application
 Dim olNS As Outlook.Namespace
 Dim olFldr As Outlook.MAPIFolder
 Dim olItem As Object
 Dim olMailItem As Outlook.MailItem
 Dim ws As Worksheet
 Dim iRow As Long
 Dim hdr As Variant
[COLOR=#FF0000] Dim AttachmentName As String[/COLOR]
 Set ws = ThisWorkbook.Worksheets("IHF ADMIN MAILING")
 Set olApp = New Outlook.Application
 Set olNS = olApp.GetNamespace("MAPI")
 Set olFldr = olNS.Folders("IHF ADMIN MAILING")
 Set olFldr = olFldr.Folders("Inbox")
 Application.ScreenUpdating = False
 ws.Cells.Clear
 iRow = 2
 With ws
 hdr = Array("Sender", "Sender Email Address", "Sender Name", "Subject", "Received Time", "Categories", "Task Completed Date", "Folder", "A[COLOR=#FF0000][/COLOR][COLOR=#FF0000]ttachments", "[/COLOR]")
 .Range("A1").Resize(, UBound(hdr)) = hdr
 End With

 For Each olItem In olFldr.Items
 If olItem.Class = olMail Then
[COLOR=#FF0000] AttachmentName = ""[/COLOR]
 Set olMailItem = olItem
 With olMailItem
 [COLOR=#FF0000]
 If .Attachments.Count > 0 Then
    For x = 1 To .Attachments.Count
        AttachmentName = AttachmentName & .Attachments.Item(x).DisplayName & ", "
    Next x
    AttachmentName = Left(AttachmentName, Len(AttachmentName) - 2)
End If[/COLOR]
    
 
 ws.Cells(iRow, "A") = .Sender
 ws.Cells(iRow, "B") = .SenderEmailAddress
 ws.Cells(iRow, "C") = .SenderName
 ws.Cells(iRow, "D") = .Subject
 ws.Cells(iRow, "E") = .ReceivedTime
 ws.Cells(iRow, "F") = .Categories
 ws.Cells(iRow, "G") = .TaskCompletedDate
 ws.Cells(iRow, "H") = olFldr.Name
[COLOR=#FF0000] ws.Cells(iRow, "I") = AttachmentName
 [/COLOR]
 iRow = iRow + 1
 End With
 End If
 Next olItem
 With ws
 .Columns.AutoFit
 End With
 Application.ScreenUpdating = False
 MsgBox "IHF ADMIN MAILING BOX - Analysis Complete!"
 End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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