Search for Outlook emails that need my action

I get hundreds of Outlook (version 2010 is what I use) emails per day but in most cases, the emails
don't need any action from me.

However, emails which require inputs/actions from me usually contains in the first 1-5 lines of it's
message body the following:

a) "Hi Pete" (more than 95% of the cases)
b) "Pete"
c) "Hello Pete"


Q1:
If I press Shift-Ctrl-F to search in the "message header & body", what should I type in as the search
keyword?  Is it  "Hi Pete" (ie with the double quotes to enclose it) or simply  Hi Pete (without double
quotes)?

Q2:
is there any way I can perform a search of Outlook on only the first 5 lines of the message content
to search for the keyword  "Pete"?

Q3:
Is there any way to be auto-notified if an email contain the word "Pete" in its message body were
sent to me rather than manually do regular check for new emails with the keyword "Pete" in it?
sunhuxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

omgangIT ManagerCommented:
Q1:  Hi Pete will work without the quotes

Q2:  Yes - custom macro/code

Q3:  Yes - custom macro/code

OM Gang
omgangIT ManagerCommented:
I'd write two different procedures so that they fulfil both Q2 & Q3.

The first procedure would check message body, first 5 lines (probably will need to tweak this) for the key words you want.
The second procedure would monitor the Inbox for new messages and call the first procedure passing the message (or just message body?) as a parameter.

OM Gang
omgangIT ManagerCommented:
See this PAQ (I answered not too long ago) re. monitoring Inbox for messages as they arrive.
http://www.experts-exchange.com/Software/Office_Productivity/Q_28651308.html

I'll see what I can come up with for checking the message body for key words.
OM Gang
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sunhuxAuthor Commented:
http://www.experts-exchange.com/Software/Office_Productivity/Q_28651308.html

think the above does not quite meet my need.  Do guide me with how to install
DIM subroutines etc as I've never used it
omgangIT ManagerCommented:
You have messages arriving in your Inbox and you want to analyze them, as they arrive, to determine if they contain specific keywords and so require your attention.

The link to the PAQ shows how to monitor a mailbox folder and do something when new items are added, e.g. a new message arrives.

I'll post up some specific examples.  Are you not familiar at all with VBA coding and/or working with the VBE (Visual Basic Editor) in Outlook?
OM Gang
omgangIT ManagerCommented:
Step1:  Outlook 2010
Set Macro Security:  Right click on some blank area in the ribbon, choose Customize the Ribbon.  In the dialog box that opens, on the right hand side, check the box next to the Developer ribbon group.

You should now have a Developer tab on the ribbon.

Click the Developer tab on the ribbon and then choose Macro Security.  Change the security level.  I set mine for Notifications for all Macros.

Macro Security dialog

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
omgangIT ManagerCommented:
Step2:  Add code to monitor your Inbox

From the Developer tab on the ribbon click the Visual Basic button.  This will open the VBE.
On the left side, in the Project browser, expand the project, then expand the folder Microsoft Outlook Objects.  Double-click the module ThisOutlookSession.

On the right side, in the code window, type (or copy and paste) the following commands.  Don't forget to save your work; you'll be prompted when you close Outlook to save changes to the VBA project.

Option Explicit

Dim WithEvents olkFolder As Outlook.Items


Private Sub Application_Startup()
On Error GoTo Err_Application_Startup

        'Change the folder path to that of the folder you want to monitor - specify display name as seen in navigation pane
    'Set olkFolder = OpenOutlookFolder("SomeAttachedMailbox\Inbox").Items
    Set olkFolder = Session.GetDefaultFolder(olFolderInbox).Items
    
Exit_Application_Startup:
    Exit Sub

Err_Application_Startup:
    MsgBox Err.Number & ", " & Err.Description, , "Error in Sub Application_Startup of VBA Document ThisOutlookSession"
    Resume Exit_Application_Startup
    
End Sub


Private Sub olkFolder_ItemAdd(ByVal Item As Object)
On Error GoTo Err_olkFolder_ItemAdd

    
    

Exit_olkFolder_ItemAdd:
    Exit Sub

Err_olkFolder_ItemAdd:
    MsgBox Err.Number & " (" & Err.Description & ") in procedure olkFolder_ItemAdd of VBA Document ThisOutlookSession"
    Resume Exit_olkFolder_ItemAdd

End Sub


Private Sub Application_Quit()
On Error GoTo Err_Application_Quit

        'destroy in-memory object variable
    Set olkFolder = Nothing

Exit_Application_Quit:
    Exit Sub

Err_Application_Quit:
    MsgBox Err.Number & " (" & Err.Description & ") in procedure Application_Quit of VBA Document ThisOutlookSession"
    Resume Exit_Application_Quit

End Sub

Open in new window

omgangIT ManagerCommented:
Once you have completed the above you'll be actively monitoring your Inbox but there's nothing specified to do so we need to work on that next.  Specifically, we need to check each message when it arrives to see if it contains the specified keywords in the first 5 lines of the message body.  If so, we'll pop up a message box.

OM Gang
omgangIT ManagerCommented:
Step3:  Parsing routine

Add the following function below the existing code you've already added.

Public Function Parse2(strSubject As String) As Boolean
On Error GoTo Err_Parse2

    Dim blResult As Boolean
    Dim lngPos1 As Long, lngPos2 As Long, lngFound As Long
    Dim intCount As Integer
    Dim strSearch1 As String, strSearch2 As String, strFiveLines As String
        
        'first we'll look for carriage return
    strSearch1 = vbCrLf
        'we'll look for the following prior to the fifth carriage return
    strSearch2 = "Pete"
    
    lngPos1 = 1
    intCount = 1
    
RECURSE:
        'find carriage return in string -- search Help (or Google) InStr function
    lngPos2 = InStr(lngPos1, strSubject, strSearch1)

    If lngPos2 <> 0 Then
            'we get here if a carriage return is found so we begin counting the number of carriage returns
            'add one to the count
        intCount = intCount + 1
            'set our new start position to be one more than the found carriage return
        lngPos1 = lngPos2 + 1
            'we'll only keep looking for carriage returns until we've found 5
        If intCount < 6 Then GoTo RECURSE
    Else
            'we get here if no carriage return found so we'll specify max length as full string length
        lngPos2 = Len(strSubject)
    End If
    
        'now we search for the keyword but limit scope to the the number of characters in the first 5 lines
    strFiveLines = Mid(strSubject, 1, lngPos2)
        'search for keyword
    lngFound = InStr(strFiveLines, strSearch2)
        'if result is not zero then we found it
    If lngFound <> 0 Then blResult = True

Exit_Parse2:
    Parse2 = blResult
   Exit Function

Err_Parse2:
   MsgBox Err.Number & " (" & Err.Description & ") in procedure Parse2 of Module Module3"
   Resume Exit_Parse2
   
End Function

Open in new window

omgangIT ManagerCommented:
Now we just need to tie it all together.

Step4:  modify the existing olkFolder_ItemAdd subroutine

Private Sub olkFolder_ItemAdd(ByVal Item As Object)
On Error GoTo Err_olkFolder_ItemAdd

    Dim blAlert As Boolean
    Dim strBody As String, strAlert As String

        'we are only going to process messages, e.g. no calendar invites, etc.
    If Item.MessageClass = "IPM.Note" Then
            'get message body into string variable
        strBody = Item.Body
            'call parsing routine to see if this message needs attention
        blAlert = Parse2(strBody)
            'evaluate function return value
        If blAlert Then
                'message needs to be addressed
            strAlert = "Yo Pete!  You got a live one" & vbCrLf  & vbCrLf & Item.Subject
            MsgBox strAlert, vbCritical, "Message Needs Attention"
        End If
    
Exit_olkFolder_ItemAdd:
    Exit Sub

Err_olkFolder_ItemAdd:
    MsgBox Err.Number & " (" & Err.Description & ") in procedure olkFolder_ItemAdd of VBA Document ThisOutlookSession"
    Resume Exit_olkFolder_ItemAdd

End Sub

Open in new window

omgangIT ManagerCommented:
I tested the parsing routine by passing the following as a sample message body (I added some Debug.Print statements so I could see where the carriage returns were and to make sure the result was accurate.

?Parse2(" 1  " & vbCrLf & "  2  " & vbCrLf & "  this is third line" & vbCrLf & "    Hi Pate   " & vbCrLf & " this is the fifth line of text" & vbCrLf & "    and the is the sixth -- Pete  ")
 5
 12
 34
 50
 83
 1  <----  first line in message body (including extra spaces)
  2  <--- second line in message body (including extra spaces)
  this is third line  <--- third line
    Hi Pate   <--- fourth line - intentional mis-spelling of your name
 this is the fifth line of text  <-- fifth line

False  <--- function return value False - Pete was not found

Note that the sixth line isn't processed even though it includes the keyword Pete.


Ran it again with your name spelled correctly
?Parse2(" 1  " & vbCrLf & "  2  " & vbCrLf & "  this is third line" & vbCrLf & "    Hi Pete   " & vbCrLf & " this is the fifth line of text" & vbCrLf & "    and the is the sixth -- Pete  ")
 5
 12
 34
 50
 83
 1  <----  first line in message body (including extra spaces)
  2  <--- second line in message body (including extra spaces)
  this is third line  <--- third line
    Hi Pete   <--- fourth line
 this is the fifth line of text  <-- fifth line

True  <--- function return value True - Pete was found


OM Gang
sunhuxAuthor Commented:
>Are you not familiar at all with VBA coding and/or working with the VBE (Visual Basic Editor) in Outlook?
Yes, not familiar at all.  Don't even know how to get to the orangish "Trust Centre" screen which you posted above
omgangIT ManagerCommented:
In that post I provided instructions for how to get there.

First, right-click onto a blank section of the ribbon.  Choose 'Customize the Ribbon'

Outlook - customize ribbon
omgangIT ManagerCommented:
Now you should have a Developer tab on your ribbon.

Outlook - Developer tab
omgangIT ManagerCommented:
Now, click the Marco Security ribbon command to get the dialog box I posted above.

If you need help with Step 2 (I posted above) let me know.

OM Gang
sunhuxAuthor Commented:
Gee, thanks very much.   I'm just discharged from hospital, not too well yet;
give me till this Tue evening to digest
omgangIT ManagerCommented:
@sunhux, hoping you are better soon and sorry to hear you were in hospital.  Take your time and let me know if you have questions.
OM Gang
sunhuxAuthor Commented:
Hi OM Gang,  got a bit of issue: refer to attached: the Macro Security box is greyed out in
my Outlook though I login using local administrator.
GreyedOutlook-MacroSecurity.jpg
omgangIT ManagerCommented:
You need to check the 'Developer' option.  Doing so will add a Developer tab to your ribbon in Outlook.  Then you change your Macro Security settings as in my 5th post above.
OM Gang
sunhuxAuthor Commented:
Thanks very much
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
Outlook

From novice to tech pro — start learning today.