lms
05-02-2014, 03:06 PM
Through someone else techically, I have this code that when I receive an email message, if the message area has an email address in it, this code recognizes the first email address in the body field of the message, and searches automatically for the contact in my list of contacts that has the email address, and it opens up the contact. The purposes of this is sometimes I send and an email message and receive a message back that a certain email was not delivered, and in the email I receive back, it shows that email address not delivered, so I can find the contact that is a part of that.
But the original code only searches the folder Contacts....not all other sub folders and sub sub folders of the Contact folder. So I changed the one line which is " Set myContacts = Session.GetDefaultFolder(olFolderContacts).Items" to a specifc sub sub folder using the following line: "Set myContacts = Session.GetDefaultFolder(olFolderContacts).Folders("Test").Folders("Sub Test").Folders("Sub Sub Test").Items", and it searches that particular sub sub folder.
So does anyone know quickly what to change so that it searches all Contacts, sub folders and sub sub folders? And if the way to do it, is I need to add to it each sub folder and sub sub folder name, I would like to know how to do that.
Thanks very much and here is the full code.
[CODE][Sub GetValueUsingRegEx32()
Dim obj As Object
Dim Selection As Selection
Dim olMail As Object 'Outlook.MailItem
Dim Reg1 As Object
Dim M1 As Object
Dim M As Object
Dim strAddress As String
Dim myContacts As Items
Dim myItem As contactItem
Set Selection = Application.ActiveExplorer.Selection
Set myContacts = Session.GetDefaultFolder(olFolderContacts).Folders("Test").Folders("Sub Test 2").Folders("Sub Sub Test 2").Items
For Each obj In Selection
Set olMail = obj
Set Reg1 = CreateObject("VBScript.RegExp")
With Reg1
.Pattern = "(([\w-\.]*\@[\w-\.]*)\s*)"
.IgnoreCase = True
.Global = False
End With
If Reg1.TEst(olMail.Body) Then
Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
strAddress = M.SubMatches(1)
Debug.Print strAddress
Set myItem = myContacts.Find("[Email1Address]=" & strAddress)
myItem.Display
Next
End If
Next
End Sub/CODE]
But the original code only searches the folder Contacts....not all other sub folders and sub sub folders of the Contact folder. So I changed the one line which is " Set myContacts = Session.GetDefaultFolder(olFolderContacts).Items" to a specifc sub sub folder using the following line: "Set myContacts = Session.GetDefaultFolder(olFolderContacts).Folders("Test").Folders("Sub Test").Folders("Sub Sub Test").Items", and it searches that particular sub sub folder.
So does anyone know quickly what to change so that it searches all Contacts, sub folders and sub sub folders? And if the way to do it, is I need to add to it each sub folder and sub sub folder name, I would like to know how to do that.
Thanks very much and here is the full code.
[CODE][Sub GetValueUsingRegEx32()
Dim obj As Object
Dim Selection As Selection
Dim olMail As Object 'Outlook.MailItem
Dim Reg1 As Object
Dim M1 As Object
Dim M As Object
Dim strAddress As String
Dim myContacts As Items
Dim myItem As contactItem
Set Selection = Application.ActiveExplorer.Selection
Set myContacts = Session.GetDefaultFolder(olFolderContacts).Folders("Test").Folders("Sub Test 2").Folders("Sub Sub Test 2").Items
For Each obj In Selection
Set olMail = obj
Set Reg1 = CreateObject("VBScript.RegExp")
With Reg1
.Pattern = "(([\w-\.]*\@[\w-\.]*)\s*)"
.IgnoreCase = True
.Global = False
End With
If Reg1.TEst(olMail.Body) Then
Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
strAddress = M.SubMatches(1)
Debug.Print strAddress
Set myItem = myContacts.Find("[Email1Address]=" & strAddress)
myItem.Display
Next
End If
Next
End Sub/CODE]