I have an excel workbook which I am using to loop through a bunch of .msg files in a folder to extract the 'sent on', 'sender' and 'subject' fields. I can extract the information but only by expressly referencing the name of the files (in this case test and test2). How do I loop through all the .msg files in the directory and extract the relevant info? This is what I have so far:
Option Explicit
Sub getMsgData()
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim mailDoc As Outlook.MailItem
Dim i As Long
i = 1
Dim nam As Variant
For Each nam In Array("test.msg", "test2.msg")
Set mailDoc = olApp.Session.OpenSharedItem(ActiveWorkbook.Path & "\" & nam)
Sheets("sheet1").Range("a1").Offset(i) = mailDoc.SentOn
Sheets("sheet1").Range("a1").Offset(i, 1) = mailDoc.Sender
Sheets("sheet1").Range("a1").Offset(i, 2) = mailDoc.Subject
mailDoc.Close False
i = i + 1
Next nam
olApp.Quit
Set mailDoc = Nothing
Set olApp = Nothing
End Sub