• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

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.
0
netcmh
Asked:
netcmh
  • 7
  • 5
1 Solution
 
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.
0
 
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>

---------------------------------------
0
 
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>"
Next
Set olkMsg = olkApp.CreateItem(olMailItem)
With olkMsg
    .Subject = MSG_SUBJECT
    .To = MSG_TO
    .BodyFormat = olFormatHTML
    .HTMLBody = "<table>" & strBuf & "</table>"
    .Send
End With
olkSes.Logoff
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
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        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
        Next
    End If
    On Error GoTo 0
End Function

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
netcmhAuthor Commented:
Thanks.

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

I was hoping to write a macro in outlook.
0
 
David LeeCommented:
Sorry, that's my fault.  Delete line 47.
0
 
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.
0
 
netcmhAuthor Commented:
Line:17
Char:1
Error: Object required
Code: 800A01A8
Source: Microsoft VBScript compilation error
0
 
David LeeCommented:
Almost certainly means that the path is invalid.  Did you edit the path?  If so, are you certain it's correct?
0
 
netcmhAuthor Commented:
So, if the folder is :

Inbox\Transfers\TransferLog

My path would be :

Const FOLDER_PATH = "Inbox\Transfers\TransferLog"

or

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

"someone@company.com\Inbox\Transfers\TransferLog"

or

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

or

"Personal Folders\Inbox\Transfers\TransferLog"

It depends on which version of Outlook you're using and where the folder is located.
0
 
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?
0
 
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.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now