Outlook macro

I would like a weekly scheduled report generated automatically.

I receive in a folder (TransferLog) emails of the same format with varying pieces of data.

Example email body:
The file Doc123.doc has been transferred by FTP into the Incoming folder

To: abc@abc.com

I would like a macro to run every Friday night at 5pm that would look inside the TransferLog folder, go through each of the email, gather a few variables and consolidate it all into one email to myself.

The email could look like this:

<date> <time> Doc123.doc FTP Incoming abc@abc.com
<date> <time> Doc1323.zip SFTP Incoming yudg@abc.com
<date> <time> Zip45.txt FTP Incoming jhhd@abc.com
<date> <time> std33.jpg SCP Incoming jhdj2@abc.com

The <date> and <time> variables are gathered from the timestamp of the emails.
The filenames could be the same or different, but will always be the 3rd object in the first line.
The mode of transfer (FTP,SCP,SFTP) could be the same or different, but will always be the 8th object in the first line.
The foldername could be the same or different, but will always be the 11th object in the first line.
The To field could be the same or different, but will always be the 2nd object in the next line.

Can some one please help with the script required to do this?

Thank you.
LVL 21
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David LeeCommented:
Hi, netcmh.

I might be able to help, but to determine that I really need to see what the body of one of these emails looks like.
netcmhAuthor Commented:
Thank you BlueDevilFan.

Here's what you asked for:

The file Сopy_of_Document_ID0500.zip has been transferred by FTP into the Incoming Folder, and has been logged.

Original Message:

  Date: Wed, 20 Aug 2014 00:32:46 -0700
  Subject: Urgent notice
  From: online_support@florida.gov
  To: <Radams@domain.org>

David LeeCommented:
This should do it assuming that messages are consistently laid out as described.  To use this

1.  Open Notepad
2.  Copy the code and paste it into Notepad
3.  Edit the code per the comments I included in it
4.  Save the file.  Name it as you see fit.  The file extension must be .vbs
5.  Using Windows Task Scheduler, create a task scheduled to run on Friday's at 5:00pm.  Set the task to run this script.  The script must run under the same account that the Outlook account exists in.

'On the next line edit the path to the folder you want the script to check
Const FOLDER_PATH = "Transfer Log"
'On the next line edit the subject line of the message the script will send
Const MSG_SUBJECT = "Some Subject"
'On the next line edit the address of the person the script is to send the message to
Const MSG_TO = "someone@company.com"
Const olMailItem = 0
Const olFormatHTML = 2

Dim olkApp, olkSes, olkFld, olkMsg, arrLin, arrWrd, strDat, strTim, strFil, strMod, strFld, strTo, strBuf

strBuf = "<tr><td>Date</td><td>Time</td><td>File</td><td>Mode</td><td>Folder</td><td>To</td></tr>"
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNamespace("MAPI")
olkSes.Logon olkApp.DefaultProfileName
Set olkFld = OpenOutlookFolder(FOLDER_PATH)
For Each olkMsg In olkFld.Items
    arrLin = Split(olkMsg.Body, vbCrLf)
    arrWrd = Split(arrLin(0), " ")
    strDat = FormatDateTime(olkMsg.ReceivedTime, vbShortDate)
    strTim = FormatDateTime(olkMsg.ReceivedTime, vbShortTime)
    strFil = arrWrd(2)
    strMod = arrWrd(7)
    strFld = arrWrd(10)
    arrWrd = Split(arrLin(2))
    strTo = arrWrd(1)
    strBuf = strBuf & "<tr><td>" & strDat & "</td>" _
                    & "<td>" & strTim & "</td>" _
                    & "<td>" & strFil & "</td>" _
                    & "<td>" & strMod & "</td>" _
                    & "<td>" & strFld & "</td>" _
                    & "<td>" & strTo & "</td></tr>"
Set olkMsg = olkApp.CreateItem(olMailItem)
With olkMsg
    .Subject = MSG_SUBJECT
    .To = MSG_TO
    .BodyFormat = olFormatHTML
    .HTMLBody = "<table>" & strBuf & "</table>"
End With
Set olkMsg = Nothing
Set olkFld = Nothing
Set olkSes = Nothing
Set olkApp = Nothing
End Sub

Function OpenOutlookFolder(strFolderPath)
    ' Purpose: Opens an Outlook folder from a folder path.
    ' Written: 4/24/2009
    ' Author:  David Lee
    ' Outlook: All versions
    Dim arrFolders, varFolder, bolBeyondRoot
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = olkSes.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
    End If
    On Error GoTo 0
End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

netcmhAuthor Commented:

When running the script, I got the Windows Script Host popup
Error: Expected Statement
Code: 800A0400
Source: Microsoft VBScript compilation error

I was hoping to write a macro in outlook.
David LeeCommented:
Sorry, that's my fault.  Delete line 47.
David LeeCommented:
I can write the macro to run inside of Outlook, but Outlook does not have a built-in means of scheduling a macro to run at a certain time.  Windows does and since you wanted this to run on a schedule I wrote it to run outside of Outlook where it can use Windows' built-in task scheduler.  I can rewrite it to run from inside of Outlook, but to make that work requires more code.  More code means a greater chance of problems.  It's your call.
netcmhAuthor Commented:
Error: Object required
Code: 800A01A8
Source: Microsoft VBScript compilation error
David LeeCommented:
Almost certainly means that the path is invalid.  Did you edit the path?  If so, are you certain it's correct?
netcmhAuthor Commented:
So, if the folder is :


My path would be :

Const FOLDER_PATH = "Inbox\Transfers\TransferLog"


Const FOLDER_PATH = "Transfers\TransferLog"
David LeeCommented:
The path needs the name of the store at the beginning.  For example



"Mailbox - Doe, John\Inbox\Transfers\TransferLog"


"Personal Folders\Inbox\Transfers\TransferLog"

It depends on which version of Outlook you're using and where the folder is located.
netcmhAuthor Commented:
Wow, this works. Thank you.

If I were to invoke verbose logging on the transfers, the 6th line's 2nd word is of interest to me.

Can I modify the arrWrd = Split(arrLin(2)) variable to arrWrd = Split(arrLin(6)) and have it pick up the variable?
David LeeCommented:
You're welcome.

Close.  Change it to

arrWrd = Split(arrLin(5))

The array starts at element 0 so the sixth line is the 5th element in the array.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.