You are here:

Excel/Macro for listing .msg files details


screen Shot
screen Shot  
Hi Damon,

As suggested by you previously, I have added a back slash(\) at the end of the file path.
But if I choose TRUE i.e. select subfolders as well, it is showing me duplicate values. Also, instead of the file path, is it possible to show the file name,(e.g.ABC.msg)?

I am hereby attaching the screen shot for your better understanding. And the code so far used is as follows:
Dim iRow    As Long
Dim MyOutlook  As Object
Dim msg        As Outlook.MailItem
Dim NS         As Namespace
Dim fs         As Object

Sub ListFiles()

  Set MyOutlook = New Outlook.Application
  Set NS = MyOutlook.GetNamespace("MAPI")
  Set fs = CreateObject("Scripting.FileSystemObject")
  iRow = 11

  Call ListMyFiles(Range("C7"), Range("C8"))

End Sub

Sub ListMyFiles(mySourcePath As String, IncludeSubfolders As Boolean)

  Dim myFile     As String
  Dim f          As Object
  Dim File       As Object
  Set f = fs.GetFolder(mySourcePath)

  On Error Resume Next

  For Each File In f.Files
     If File.Name Like "*.msg" Then

        Set msg = NS.OpenSharedItem(mySourcePath & File.Name)
         Cells(iRow, 2).Value = mySourcePath
         Cells(iRow, 3).Value = msg.Subject
         Cells(iRow, 4).Value = msg.To
         Cells(iRow, 5).Value = msg.SentOn
         iRow = iRow + 1
     End If

  Next File

  If IncludeSubfolders Then
     Dim mySubFolder      As Object

      For Each mySubFolder In f.SubFolders

        Call ListMyFiles(mySubFolder.Path, True)


  End If

End Sub

Hi again Idrani,

To show the file names in column B change code line

 Cells(iRow, 2).Value = mySourcePath


 Cells(iRow, 2).Value = File.Name

Regarding the duplicate listings, I have no idea why this is happening, and have not been able to duplicate it on my computer.  However, I will ponder it a bit and will try to get back to you tomorrow.  Perhaps I can think of some reason why this is happening or some test that could tell us why.

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Damon Ostrander


I have extensive experience with VBA programming in Excel 5 through Excel 2013. As a former aerospace engineer with a large aerospace corporation and consultant in a small defense technology services company, I have developed a wide range of applications in VBA, including simulations involving mixed-language programming, satellite orbit mechanics, graphics and animation, and real-time applications. I am interested in moderate to hard VBA-related questions only.


I have developed and taught several courses in Excel VBA programming and also VBA programming in Office 97, 2000, and 2007. I have developed a number of large technical applications in Excel VBA for use within the aerospace industry.

B.S. in Electrical Engineering and Computer Science, University of California, Berkeley.

©2017 All rights reserved.