Outlook 2007 Search Through E-Mail

Status
Not open for further replies.

LMS

Senior Member
Outlook version
Email Account
Exchange Server
Recently we came up with the way to move an email coming from Microsoft as a non-delivery to a folder I want to move it to, as I can select a lot and move them without having to find the folder and drag it.

So when I open that email, it simply in the note field it has the email that did not work.

Is there a macro that I select an email, just select it, and then run the macro and it searches all contact folders and subfolders etc. to find the contact that comes with that email.

That would be great. Thanks.
 
Your macro not for selecting an email address in the note field of a email from some other place....and I just want to have it open the contact...not do anything else.....and ideas please?
 
change this block to do whatever you want
Code:
myItem.Categories = myItem.Categories & ";Delete" 
 
myItem.Save

to open the contact, use
myitem.display
 
Your macro not for selecting an email address in the note field of a email from some other place....and I just want to have it open the contact...not do anything else.....and ideas please?
 
Where do i put the line code: myitem.display? And will this work selecting an email address? And if so, how search in the sub folders and the sub sub folders all at one time?
 
The macro works on the selected message, you don't select the email address in the message, the regex looks for the first address.

Change myItem.Save to myItem.display to open it.
 
Thanks but that is not what I need. As the message is from Microsoft not the email address it did not go to. I want to find the contact that is the email address in the message as the message says that email address did receive my email. Anyway to do this?
 
did you look at the screenshot on that page with the example of an NDR? Does your NDR have the email address in the message body like in the screenshot? The text can be different - as long as the email address is in the body, this line in the code will find it: .Pattern = "(([\w-\.]*\@[\w-\.]*)\s*)"

The code looks for the first email address in the body.
 
I did the following per the NDR you mentioned....tested it to a contact in the Contact folder, and it opened up the contact....so what can we now change so it searches all contact folders, subfolders, and sub sub folders please?

Sub GetValueUsingRegEx3()
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).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)
'If TypeName(myItem) = "ContactItem" Then
' If Not TypeName(myItem) = "Nothing" Then
'myItem.Categories = myItem.Categories & ";Delete"
' Debug.Print strAddress & " Delete"
myItem.Display
'End If
'End If
Next
End If
Next
End Sub
 
The following code is that I had it search a specific sub sub folder called "Sub Test" which is below the sub folder called "Test", and it works to the folder...but how do we do for all sub and sub sub folders....or if I want to add another folder to this list, how do I just add another folder or more folders as I tried it just putting the same words to another folder after the &...but that did not work...so I can add all folders to it if that's the only way to do it...so need to search more than one folder: Thanks much

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").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
 
This is one reason we recommend using Categories and views over a lot of folders. :)

I don't know if its better to use a GetFolder sub or a different method to find the contact.
 
I would like to change the code to search all folders or the names of the folders I add. How to do it either way please.
 
At my office needing do it ...any update? As I have a lot of emails back etc.
 
You need to use something like

set objFolder =Session.GetDefaultFolder(olFolderContacts)

For Each fld in objFolder.Folders
Set myContacts = objFolder.Folders.Items
Set myItem = myContacts.Find("[Email1Address]=" & strAddress)
Next

if you have contacts\subfolder\subfolder format, it's more complicated because the above code only looks at the folders under contacts.

The Private Sub LoopFolders code sample at http://www.vboffice.net/en/developers/expand-all-folders could be used but it needs some editing to work in your search and i definitely don't have time to do that.
 
Here is the code that I use for sub sub folders but the same code for sub folders.....where do I put what you wrote and were ..and what do I delete....can you just adjust the code for me please:

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").Folders("Sub Sub Test").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
 
Try something like this - I can't test it because i don't have subfolders, but it should work.

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 
 
Dim fld as Outlook.folder 
Dim objFolder as Outlook.folder 
 
Set Selection = Application.ActiveExplorer.Selection 
 
' parent folder 
Set objFolder = Session.GetDefaultFolder(olFolderContacts) 
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 
 
For Each fld in objFolder.Folders 
Set myContacts = objFolder.Folders.Items 
 
Set myItem = myContacts.Find("[Email1Address]=" & strAddress) 
myItem.Display 
 
Next 
 
Next 
 
End If 
Next 
End Sub
 
There is an error where the following line turns yellow and it does not do it:

Set myContacts = objFolder.Folders.Items
 
What's the error message? It's most likely something with the line before it:
For Each fld in objFolder.Folders

try changing these lines to
Dim fld as Object
Dim objFolder as Object
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J outlook 2007 doesn't let me choose which .pst to search Using Outlook 2
S outlook 2007 calendar search Using Outlook 6
S Outlook 2007 Calendar instant search problem. Windows 7 Using Outlook 4
S Outlook 2007 Calendar instant search problem. Windows 7 Using Outlook 0
L Outlook 2007 - Macro Re Search Using Outlook 16
L Outlook 2007 Criteria Search Using Outlook 9
L Outlook 2007 Search All Fields in Contacts Using Outlook 1
L Outlook 2007 Macro Search Contacts Using Outlook 9
L Outlook 2007 Search Folder Using Outlook 1
Z outlook 2007 - opening a contact from the search results doesn't use the custom form Using Outlook 10
M Search emails not working in Outlook 2007 Using Outlook 1
G Outlook 2007 compound (OR) instant search break down Using Outlook 14
A Outlook 2007 message search results loses focus Using Outlook 7
L Outlook 2007 E-Mail Search Using Outlook 9
L Outlook 2007 E-mail Search Using Outlook 19
P Disable Instant Search in Outlook 2007 Outlook VBA and Custom Forms 9
T Outlook 2007 Indexing Options and Advanced Search options disabled Using Outlook 5
D Outlook 2007 Recovering E-Mails Using Outlook 0
W Transfer Outlook 2016 autocomplete file to Outlook 2007 Using Outlook 1
C Outlook 2007 Removing then adding account restores junk email processing Using Outlook 0
S Outlook 2007 crash linked to gdiplus.dll Using Outlook 0
S Outlook 2007 - Automatic purge fail Using Outlook 0
D Outlook 2007 vs. Outlook 2010 -- ToDo Bar Using Outlook 0
D Outlook 2007 on 365 Using Outlook.com accounts in Outlook 2
S Macro for other actions - Outlook 2007 Outlook VBA and Custom Forms 23
S Verwendung von Outlook 2007 Using Outlook 0
A Arthur needs help with 2007 Outlook e-mail Using Outlook.com accounts in Outlook 3
M PST import from Outlook 2007 to 2010 - Address Book contacts all in 1 group Using Outlook 4
B Migrate Outlook 2007 to Office 365 Using Outlook 3
X I have met my waterloo trying to resolve embedded graphics problem with outlook 2007 and now 2016 Using Outlook 1
R Outlook 2007 only loads some appointments Using Outlook 0
C Move Outlook 2007 to new PC with Outlook 365 Using Outlook 3
J Outlook 2007 Hide Messages Option not Available Using Outlook 2
B Server errors Outlook 2007 Using Outlook 1
S Reboot of frozen windows7 results in changed outlook 2007 settings Using Outlook 1
S Outlook 2007 printing wrong email address at top of page Using Outlook 8
M Configure outlook 2007 to accept digital signatures Using Outlook 2
D Outlook 2007 crashes when opening an email Using Outlook 2
R New chap saying hello and needing advice on Outlook 2007 thumbnails Using Outlook 3
icacream From Outlook 2007 to 2016 ! Using Outlook 9
vodkasoda Object could not be found Error in Outlook 2007 Outlook VBA and Custom Forms 5
S Outlook 2007: Address Cards allow entering text! Why? Using Outlook 3
S View Appointment in Text Wrap in Outlook 2007 Month Calendar View Using Outlook 0
L Outlook 2007 Separate the Send/Receive functions Using Outlook 2
M Outlook 2007 Contacts Glitch: Creating a new email Using Outlook 1
C Move from Outlook 2007 Enterprise (MOE) to Outlook Pro plus 2007 Using Outlook 1
J reinstalling Outlook 2007 asking for user name & password Using Outlook 14
P outlook addin unloaded in office 2007 Using Outlook 0
B Fonts in Outlook 2007 Using Outlook 4
R Add Exchange Account to existing POP3 Outlook 2007 Profile Using Outlook 0

Similar threads

Back
Top