2

I have a folder with thousands of Outlook .msg files.

I'd like to know if it's possible to write a VB Script that can read the sender and receiver from each file, and move the .msg file to a folder based on this info?

Thanks

gers1978
  • 21
  • 1
  • 3

2 Answers2

2

You shouldn't ask yes/no questions unless you expect the answer to be either "yes" or "no".

Set ol  = CreateObject("Outlook.Application")
Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("C:\some\folder").Files
  If LCase(fso.GetExtensionName(f)) = "msg" Then
    Set msg = ol.CreateItemFromTemplate(f.Path)
    WScript.Echo msg.Sender.Name
    For Each rcpt In msg.Recipients
      WScript.Echo rcpt.Name
    Next
  End If
Next
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

For reading the contents of a .msg file, I have used the following approach.

  1. use CreateItemFromTemplate for the msg file using the outlook object
  2. Use this outlook object to save the data into a .txt file
  3. Once the .txt file is created read it and use the data as required

Script:

Dim OL : Set OL=CreateObject("Outlook.Application")
Dim Msg ':Set Msg= CreateObject("Outlook.MailItem")
Set Msg = OL.CreateItemFromTemplate("C:\test.msg")
'MsgBox Msg.Subject
Msg.saveAs "C:\test.txt", olDoc
'The above statement will save the contents of .msg file into the designate .txt file 

Set OL = Nothing
Set Msg = Nothing

Once the .txt file is created use it as required for your computations.

Matthias
  • 3,582
  • 2
  • 30
  • 41