Outlook macro

Posted on 2014-08-19
Last Modified: 2014-08-21
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


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
<date> <time> SFTP Incoming
<date> <time> Zip45.txt FTP Incoming
<date> <time> std33.jpg SCP Incoming

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.
Question by:netcmh
    LVL 76

    Expert Comment

    by:David Lee
    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.
    LVL 20

    Author Comment

    Thank you BlueDevilFan.

    Here's what you asked for:

    The file С 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
      To: <>

    LVL 76

    Accepted Solution

    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 = ""
    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

    LVL 20

    Author Comment


    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.
    LVL 76

    Expert Comment

    by:David Lee
    Sorry, that's my fault.  Delete line 47.
    LVL 76

    Expert Comment

    by:David Lee
    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.
    LVL 20

    Author Comment

    Error: Object required
    Code: 800A01A8
    Source: Microsoft VBScript compilation error
    LVL 76

    Expert Comment

    by:David Lee
    Almost certainly means that the path is invalid.  Did you edit the path?  If so, are you certain it's correct?
    LVL 20

    Author Comment

    So, if the folder is :


    My path would be :

    Const FOLDER_PATH = "Inbox\Transfers\TransferLog"


    Const FOLDER_PATH = "Transfers\TransferLog"
    LVL 76

    Expert Comment

    by:David Lee
    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.
    LVL 20

    Author Comment

    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?
    LVL 76

    Expert Comment

    by:David Lee
    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.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    My experience with Windows 10 over a one year period and suggestions for smooth operation
    Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now