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 |
|
Dmitry Kostochko
Add-in Express team
Posts: 2875
Joined: 2004-04-05
|
|
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. |
|
Andrei Smolin
Add-in Express team
Posts: 18817
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.
Andrei Smolin
Add-in Express Team Leader |
|