Filter based on subject Line

Add-in Express™ Support Service
That's what is more important than anything else

Filter based on subject Line
 
Sri Potluri




Posts: 14
Joined: 2016-05-13
Hi,

I have to write code to filter all my outlook folders looking for a specified format of subject line as shown below.
GSS**-***-16-4027-15GSS** Opportunity Info

Here the stars are certain Alphabet codes which we have in our system (eg:CM, SOM etc) and the other numeric parts of the string may vary too. The only constant is "GSS"
I tried restrict and find methods but look like they all look for the exact string.
I am trying to loop through all emails and use a filter condition like below but it retrieves all items with GSS in subject line. Can you please suggest if there's a way to achieve what I am looking for(using wildcards etc)

If (currentFolder.DefaultItemType = Outlook.OlItemType.olMailItem) Then
folderItems = currentFolder.Items
For Each item As Outlook.MailItem In folderItems
If (item.Subject IsNot Nothing And item.Subject.Contains("GSS")) Then
'Save email as .msg file
End If
Next
End If

Thank you so much,
Sri
Posted 20 Jan, 2017 16:56:42 Top
Dmitry Kostochko


Add-in Express team


Posts: 2731
Joined: 2004-04-05
Hi Sri,

I think you can try to use the Microsoft Jet query language syntax instead of looping through all emails. Please have a look at the following MSDN articles, hope they will be helpful:
Filtering Items
Items.Find Method
Filtering Items Using Query Keywords

BTW, you can try to use the Application.AdvancedSearch method if your code does not need to handle items immediately.
Posted 23 Jan, 2017 07:50:41 Top
Sri Potluri




Posts: 14
Joined: 2016-05-13
Thanks Dmitry,
I tried the Find and Restrict methods but I am looking for a specific pattern in the subject line but I saw this on MSDN webpage.

"For example, you cannot use Find or Restrict to search for items that have a particular word in the Subject field. Instead, you can use the AdvancedSearch method, or you can loop through all of the items in the folder and use the InStr function to perform a search within a field."
Hence, I am looping through emails like below and get the result

For Each item In folderItems
If ((InStr(item.Subject, "GSS") = 1) And (item.Subject Like "GSS*GSS*")) Then
Dim endPos As String = item.Subject.ToString.IndexOf(" ")
Dim filePath As String = String.Format("C:\EmailFiles\" + item.Subject.Substring(0, endPos) + ".msg")
item.SaveAs(filePath, Outlook.OlSaveAsType.olMSG)
End If
Next

However, if I use
For Each item As Outlook.MailItem In folderItems
instead of
For Each item In folderItems
The control does not enter the IF loop. Can you please tell me what is causing this difference in behavior.
Posted 23 Jan, 2017 13:44:09 Top
Andrei Smolin


Add-in Express team


Posts: 14140
Joined: 2006-05-11
Hello Sri,

I can't tell why this issue occurs. Note however that we suggest *not* using ForEach. Below is a citation from section Releasing COM objects at https://www.add-in-express.com/docs/net-office-tips.php#releasing:


Don't use ForEach loops on COM collections.

Such a loop accesses the collection's enumerator internally. The enumerator is a COM object and this is the root of the problem. Use For loops instead.


Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 24 Jan, 2017 06:19:40 Top