Solved

adding Filter with reference to Subject Field not working

Posted on 2016-11-08
12
15 Views
Last Modified: 2016-11-09
Hi All,

I'm new to VBA scripting but i am making progress. Slowly and in baby steps but i'm getting there.

Currently we have a personal Inbox and a shared inbox. I have set up the following script which has a Pop Up (on the shared inbox) when a new email arrives. Now i want to filter these new Emails so that the pop up ONLY comes if in the Subject: <created with Priority P0> is found. I have tried and tried but unfortunately no success.

My current script:
Private WithEvents Items As Outlook.Items

Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim FoldersArray As Variant
Dim i As Integer

On Error GoTo GetFolderPath_Error
If Left(FolderPath, 2) = "\\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "\")
Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
If Not oFolder Is Nothing Then
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = oFolder.Folders
Set oFolder = SubFolders.Item(FoldersArray(i))
If oFolder Is Nothing Then
Set GetFolderPath = Nothing
End If
Next
End If
'Return the oFolder
Set GetFolderPath = oFolder
Exit Function

GetFolderPath_Error:
Set GetFolderPath = Nothing
Exit Function
End Function

Private Sub Application_Startup()
  Dim Ns As Outlook.NameSpace
  Set Ns = Application.GetNamespace("MAPI")
  Set Items = GetFolderPath("+support\Inbox").Items
End Sub
  
Private Sub Items_ItemAdd(ByVal Item As Object)
 On Error Resume Next
   MsgBox "new message  from " & Item.SenderName & " in shared folder"

End Sub

Open in new window

Ideally i would like the Pop Up to read :
New P0 has been opened. Please contact Customer ASAP

The Pop Up works perfectly, but currently this pops up everytime an email is received in the shared inbox.

Would there be a way to add the Ticket Nr to the MsgBox ?

Any help regarding this would be appreciated.

Many thanks
daniel
0
Comment
Question by:Daniel Hausdorf
  • 7
  • 4
12 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 41880008
Of course you can process and  extract the priority from the subject and display that. Same with Ticket Number. But to provide code, we need an example mail.
0
 

Author Comment

by:Daniel Hausdorf
ID: 41880013
Hi Qlemo,

many thanks for the response.

The Subject Field of an Incoming EMail looks like this:

Ticket INC00125742 created with Priority P0 Not Acknowledged

As said these come in to our Shared Mailbox.

Many thanks in advance.
Daniel
0
 

Author Comment

by:Daniel Hausdorf
ID: 41880015
Thanks for that :)
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 41880030
Private Sub Items_ItemAdd(ByVal Item As Object)
Dim ticket as String
Dim prio as String
  On Error Resume Next
  If Item.Subject like "Ticket * created with Priority *" Then
    ticket = MId(item.Subject, 7, 11)
    prio = Mid(item.Subject, len("Ticket INC00125742 created with Priority ", 2)
    MsgBox "New " & prio & " ticket " & ticket & " has been opened. Please contact Customer ASAP"
  End If
End Sub

Open in new window

This is not robust against subject line changes, and requires fixed positions and lengths for ticket no. and priority. More flexible approachs are more complex.
0
 

Author Comment

by:Daniel Hausdorf
ID: 41880036
Hi Qlemo,

you are FANTASTIC ! Many thanks for the quick response and reply.
Looks like i'm getting a 'Compile Error' at
Line 7 :  prio = Mid(item.Subject, len("Ticket INC00125742 created with Priority ", 2)

I will try to figure this out.
Do i add this script under the Application_Startup ?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41880041
My code snippet replaces the same SUB in your code (last 4 lines there). And I've left out a closing bracket (typing on tablet, so cannot test):
Private Sub Items_ItemAdd(ByVal Item As Object)
Dim ticket as String
Dim prio as String
  On Error Resume Next
  If Item.Subject like "Ticket * created with Priority *" Then
    ticket = MId(item.Subject, 7, 11)
    prio = Mid(item.Subject, len("Ticket INC00125742 created with Priority "), 2)
    MsgBox "New " & prio & " ticket " & ticket & " has been opened. Please contact Customer ASAP"
  End If
End Sub

Open in new window

0
 

Author Comment

by:Daniel Hausdorf
ID: 41880045
Cheers !

Found the missing bracket aswell.
I will test this and let you know.
Many many thanks for your help ! Much appreciated
Cheer
daniel
0
 

Author Comment

by:Daniel Hausdorf
ID: 41880058
Hi Olemo,

your script works perfectly !

Seeing that the Ticket Number will always have a new number, is there a way to have the ticket number as a variable in the script ?

Line7 : prio = Mid(item.Subject, len("Ticket INCXXXXXXXX created with Priority ")

Cheers
Daniel
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 41880067
The ticket number is a variable already. The one in line 7 is just used as a template, to get the correct length of the overall string - you could use your X'd one, or put in something more obvious like INC12345678, or leave as-is.
0
 

Author Comment

by:Daniel Hausdorf
ID: 41880073
Once again, many thanks !
0
 

Author Closing Comment

by:Daniel Hausdorf
ID: 41880074
Excellent !
Quick and fast !
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The code described here does no longer work. Please see replacement Article: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/A_3887-Getting-your-EE-Ranking-statistics-in-Excel-The-Next-Generation.html (http…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

13 Experts available now in Live!

Get 1:1 Help Now