Jump to content

[SOLVED] how to find outlook emails that are unread and have a specific subject


Recommended Posts

Good morning everyone, I thought I had already solved this issue but it turns out I did not. 

My code finds unread emails with this specific subject line of "request" but when I change the subject to SKIPPED + "request" = ("SKIPPED request") the program still finds the email and tries to process it. I only want to process emails with the exact match subject of "request".

Here is my code that "finds" the unread emails with the subject of "request" or so I thought. 

Func ListUnreadEmails()

    ;*******************************************************************************
    ; Lists all unread E-mails from the folder Outlook-UDF-Test
    ;*******************************************************************************
    ; Stores all the unRead emails into an array
    Global $aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, _
    "[UnRead]=True", "Subject", "request", "EntryID,Subject", "", 1)

    ; Displays the array of unRead emails
    If IsArray($aItems) Then
        ;_ArrayDisplay($aItems, "OutlookEX UDF: _OL_ItemFind - Unread mails")

    Else
        MsgBox(48, "OutlookEX UDF: _OL_ItemFind Example Script", _
        "Could not find an unread mail. @error = " & @error & ", @extended: " & @extended)
    EndIf

    ; Gets the number of unread emails
    Global $numberOfUnRead = UBound($aItems, $UBOUND_ROWS) - 1
    ;MsgBox("", "Number of Unread emails", $numberOfUnRead)

EndFunc

It acts as if any part of the subject containing the word "request" and the email is unread that it will try to process it. (I think)

Edited by nooneclose
Link to comment
Share on other sites

Thats correct, it's a partial match so you can't do what you want with _OL_ItemFind.
With function _OL_ItemSearch it's possible to search for unread mails that start with a specific string in the subject - unfortunately it is quite complex.

I suggest to use _OL_ItemFind to retrieve all unread mails, then search the returned array for all mails with the needed subject and ignore the rest.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water thank you for the quick reply. I was able to fix the issue with a simple if statement. 

 

; Retrieve the items object
    Global $oItem = $oOutlook.Session.GetItemFromID($aItems[1][0], Default)
    $oItem.GetInspector

    Global $eBody          = $oItem.Body                        ; The Body of the email
    Global $eSenderAddress = $oItem.SenderEmailAddress          ; Customers email address
    Global $eSentOn        = $oItem.SentOn                      ; When was the email sent?
    Global $eSubject       = $oItem.Subject                     ; Subject of the email

    if $eSubject <> "Request" Then
        ChangeEmailStatus()
    EndIf
    
Func ChangeEmailStatus()

    ;*******************************************************************************
    ; changes the status of an email from Unread to Read
    ;*******************************************************************************
    ConsoleWrite("Current email has been changed from Unread to Read." & @CRLF)
    _OL_ItemModify($oOutlook,$aItems[1][0], Default, "Unread=False")

EndFunc

I inserted an if statement to force the program to "skip" (mark as read) the emails with subjects that do not match. Thank you very much for your time. 

Link to comment
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...