PDA

View Full Version : [SOLVED:] Return metadata from Outlook .msg-files in subfolders



vegard_fv
08-14-2018, 01:13 AM
I have received 500 .msg-files/Outlook items stored in multiple folders. Each file is an export of a calendar entry in Outlook. I would like to fetch metadata from each file, and print it to a structured excel-sheet, with the columns calenderitem.text, .sender, .reciever and .date, etc.

This snippet seams to do what I'm looking for:
http://www.litigationsupporttipofthenight.com/single-post/2018/03/31/VBA-code-to-extract-metadata-from-msg-files

But I get an error message saying "Type mismatch" when the code reaches the line "Set msg = x.OpenSharedItem(Path + FileList(Row))". :crying:

When i debug.print the filearray in the code, it contains all the .msg-files, and both the variables path+filelist(row) returns correct file name. :doh:

Does anyone have a suggestion on what goes wrong, or any hints of how the job might be done?:think:

gmayor
08-14-2018, 02:08 AM
I suggest that you copy the code from
'http://www.rondebruin.nl/win/s1/outlook/openclose.htm
to open Outlook reliably. You don't need to set a reference to Outlook in the VBA project when using the following code


Option Explicit

Sub GetMailInfo()
'Use the following code to access Outlook
'http://www.rondebruin.nl/win/s1/outlook/openclose.htm
'Graham Mayor - http://www.gmayor.com - Last updated - 14 Aug 2018
Dim MyOutlook As Object
Dim msg As Object
Dim x As Object
Dim Path As String
Dim i As Long, Row As Long
Dim strFile As String

Set MyOutlook = OutlookApp()
Set x = MyOutlook.GetNamespace("MAPI")
Path = "C:\FooFolder\email\"
Row = 1
strFile = Dir$(Path & "*.msg")
While strFile <> ""
Set msg = x.OpenSharedItem(Path & strFile)
Cells(Row + 1, 1) = msg.Subject
Cells(Row + 1, 2) = msg.Sender
Cells(Row + 1, 3) = msg.CC
Cells(Row + 1, 4) = msg.To
Cells(Row + 1, 5) = msg.SentOn
If msg.Attachments.Count > 0 Then
For i = 1 To msg.Attachments.Count
Cells(Row + 1, 5 + i) = msg.Attachments.Item(i).FileName
Next i
End If
msg.Close 0
Row = Row + 1
strFile = Dir$()
Wend
End Sub

vegard_fv
08-14-2018, 04:13 AM
Thanx alot!