Search code examples
outlookvbscript

Get first unread email in Outlook inbox and fetch related properties in VBScript


I inherited this from a former team member. Currently its used to fetch attachments from unread emails, rename the attachments and store them in a specified folder (with some help from an automation tool):

Set FSO = CreateObject("Scripting.FileSystemObject")
dim mail,olMSGUnicode, subject, olHTML, saveFolder, flag, subject1, companyCode, emailAddressTo
dim dd, mm, yy, hh, mm, ss, datevalue, dtsnow
subject = WScript.Arguments.Item(0)
saveFolder = WScript.Arguments.Item(1)

'Store DateTimeStamp once.
dtsnow = Now()

'Individual date components
dd = Right("00" & Day(dtsnow), 2)
mm = Right("00" & Month(dtsnow), 2)
yy = Year(dtsnow)

'Build the date string in the format yyyy-mm-dd
'datevalue = dd & "-" & mm & "-" & yy
 datevalue = dd & mm & yy

olMSGUnicode = 9
Set app = CreateObject("Outlook.Application")
Set ns = app.GetNamespace("MAPI")
'Temp subfolder in Outlook Inbox
Set mf = ns.GetDefaultFolder(6)
set objAtt = Outlook.Attachment

Set c = mf.Items

For Each mail In c
    'Read unread email only
    If mail.unread Then 
        'Count number of mail items that have attachments
        intCount = mail.Attachments.Count
        If intCount > 0 Then
            For i = 1 To intCount
                'Save to savefolder directory
                fileName = mail.Attachments.Item(i).DisplayName
                newFileName = datevalue & "_" & fileName
                mail.Attachments.Item(i).SaveAsFile saveFolder & _
                    newFileName
            Next 
        End If
    'Mark email as read
    mail.Unread = False
    End If
Next

WScript.StdOut.WriteLine(newFileName)
                
On Error goto 0

How can I achieve the following?

  1. Get the sender email address or Exchange user for each email
  2. Have multiple WriteLine methods. This output is then captured by the automation tool for further processing
  3. Rather than looping through all unread emails at once, I would like to only fetch the first unread email. This code will be used to capture multiple requests that are sent to the mailbox intermittently throughout a given day

Sorry about the messy code or incorrect use of terminology, I'm not very familiar with VBScript to be honest.

Any guidance would be much appreciated.


Solution

  • To get the sender email address use mail.SenderEmailAddress

    Not sure about "multiple WriteLine methods" you can just put the writeline method in the loop

    To keep uniformity with your code, if you wish to read just one email rather than all at once:

    bFoundUread = false
    For Each mail In c
        'Read unread email only
        If bFoundUread = false Then
            If mail.unread Then 
                bFoundUread = true
                'Count number of mail items that have attachments
                intCount = mail.Attachments.Count
                If intCount > 0 Then
                    For i = 1 To intCount
                        'Save to savefolder directory
                        fileName = mail.Attachments.Item(i).DisplayName
                        newFileName = datevalue & "_" & fileName
                        mail.Attachments.Item(i).SaveAsFile saveFolder & _
                            newFileName
                    Next 
                End If
            'Mark email as read
            mail.Unread = False
            End If
        End If
    Next