Import .msg file body to Excel

D3allamerican07

Board Regular
Joined
Jul 22, 2015
Messages
101
I am working on a process to import a folder of .msg files into excel. Each file contains a single serial number (only thing in the body) that needs to be placed on a new row. The macro below seems to work for all parts of the message except for .Body.

Goal: Loop through folder, copy body of message, paste on new row in sheet named ("Acks").

If there is a better method, please let me know! Thank you in advance!

Code:
Sub Test()

    Dim i As Long
    Dim inPath As String
    Dim thisFile As String
    Dim ws As Worksheet
    Dim olApp As Outlook.Application
    Dim MSG As Outlook.MailItem

    Set olApp = CreateObject("Outlook.Application")
    Set ws = Sheets("Acks")

    With Application.FileDialog(msoFileDialogFolderPicker)
       .AllowMultiSelect = False
            If .Show = False Then
                Exit Sub
            End If
        On Error Resume Next
        inPath = .SelectedItems(1) & "\"
    End With
    thisFile = Dir(inPath & "*.msg")
    i = 2
    Do While thisFile <> ""
        Set MSG = olApp.CreateItemFromTemplate(inPath & thisFile)
        ws.Range("A" & i) = MSG.Body
        i = i + 1
        thisFile = Dir()
    Loop

    Set myItem = Nothing
    Set myOlApp = Nothing
    
 End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This works for me; note that I removed the “on error” statement to allow for possible error messages:

Code:
Sub Test()
Dim i&, inPath As String, thisFile$, ws As Worksheet
Dim olApp As Outlook.Application, MSG As MailItem
Set olApp = CreateObject("Outlook.Application")
Set ws = Sheets("Acks")
With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    If .Show = False Then Exit Sub
    inPath = .SelectedItems(1) & "\"
End With
thisFile = Dir(inPath & "*.msg")
i = 2
Do While thisFile <> ""
    Set MSG = olApp.CreateItemFromTemplate(inPath & thisFile)
    ws.Range("A" & i) = MSG.Body
    i = i + 1
    thisFile = Dir()
Loop
Set MSG = Nothing
Set olApp = Nothing
End Sub
 
Upvote 0
Worf,

Thanks for the reply. Any idea why I keep getting an "Run-time error 287: Application-defined or object-defined error" at the line
Code:
ws.Range("A" & i) = MSG.Body
 
Upvote 0
See if the displayed information helps you pinpoint the issue. Does the error happen on the first message?


Code:
Sub Test()
Dim i&, inPath As String, thisFile$, ws As Worksheet
Dim olApp As Outlook.Application, MSG As MailItem
Set olApp = CreateObject("Outlook.Application")
Set ws = Sheets("Acks")
With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    If .Show = False Then Exit Sub
    inPath = .SelectedItems(1) & "\"
End With
thisFile = Dir(inPath & "*.msg")
i = 2
On Error Resume Next
Do While thisFile <> ""
    Set MSG = olApp.CreateItemFromTemplate(inPath & thisFile)
    MsgBox "This file: " & thisFile & vbLf & "inPath= " & inPath & vbLf & "Subject= " & _
    MSG.Subject & vbLf & "Body= " & MSG.Body, 64, "i=" & i
    MsgBox "HTML Body= " & MSG.HTMLBody
    ws.Range("A" & i) = MSG.Body
    If Err.Number <> 0 Then
        MsgBox Err.Description, 64, Err.Number
        Exit Sub
    End If
    i = i + 1
    thisFile = Dir()
Loop
Set MSG = Nothing
Set olApp = Nothing
End Sub
 
Upvote 0
Worf,

I'm sorry for the late response. Unfortunately I am still getting an application-defined error on line:

Code:
    MsgBox "This file: " & thisFile & vbLf & "inPath= " & inPath & vbLf & "Subject= " & _
    MSG.Subject & vbLf & "Body= " & MSG.Body, 64, "i=" & i

I have since decided to manually save the email as a .txt file to avoid this approach. Thank you for the help however. It is appreciated!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,176
Messages
6,123,464
Members
449,100
Latest member
sktz

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