Search for email in Inbox and subfolders

  • Thread starter Fox via OfficeKB.com
  • Start date
F

Fox via OfficeKB.com

I have an Excel macro already written to loop through an Outlook folder
(Inbox or a subfolder I provide) and find and email based on the subject. If
the email is found it runs some other code. Right now I am the only one that
runs the macro, so I already know what Outlook folder the macro needs to
search. Others may need to run this macro though, and since they may have
the email in a different folder, the macro needs to be able to search the
Inbox, and then any subdirectories until it finds the necessary email. They
may even have copies of the email in multiple folders, but I don't think that
would make a difference since they would all be identical and the macro just
needs to pull some information from the body of the email.

I came across a thread similiar to this where Eric Legault posted some code
for looping through Outlook to list all the folders, but I couldn't find a
way to adapt it to search email in the Inbox and subfolders. Can anyone help
me with this? Thanks.
 
G

Guest

This procedure is generic enough; it will loop through all e-mails in the
passed folder, as well as within all subfolders underneath. Just call it
once with your parent folder:

e.g.
ParseSubFolders MyParentFolder

Sub ParseSubFolders(objCurrentFolder As Outlook.MAPIFolder)
Dim objItems As Outlook.Items
Dim objItem As Object, objMailItem As Outlook.MailItem

'FIND ALL E-MAIL MESSAGES IN THE CURRENT FOLDER
Set objItems = objCurrentFolder.Items
For Each objItem In objItems
If objItem.Class = olMail Then
Set objMailItem = objItem
End If
Next

For Each objCurrentFolder In objCurrentFolder.Folders
If objCurrentFolder.DefaultItemType = olMailItem Then
ParseSubFolders objCurrentFolder
End If
Next

End Sub
 
F

Fox via OfficeKB.com

I think I understand the code, but I am only looking for one mail item that
could be anywhere. Would I insert code to check the subject after the line:
Set objMailItem = objItem

Then, if the subject I'm looking for matches, would I exit the sub? Would
the mail item still be selected that I would copy the information from or do
I need to copy the information while still in this sub?
This procedure is generic enough; it will loop through all e-mails in the
passed folder, as well as within all subfolders underneath. Just call it
once with your parent folder:

e.g.
ParseSubFolders MyParentFolder

Sub ParseSubFolders(objCurrentFolder As Outlook.MAPIFolder)
Dim objItems As Outlook.Items
Dim objItem As Object, objMailItem As Outlook.MailItem

'FIND ALL E-MAIL MESSAGES IN THE CURRENT FOLDER
Set objItems = objCurrentFolder.Items
For Each objItem In objItems
If objItem.Class = olMail Then
Set objMailItem = objItem
End If
Next

For Each objCurrentFolder In objCurrentFolder.Folders
If objCurrentFolder.DefaultItemType = olMailItem Then
ParseSubFolders objCurrentFolder
End If
Next

End Sub
I have an Excel macro already written to loop through an Outlook folder
(Inbox or a subfolder I provide) and find and email based on the subject. If
[quoted text clipped - 11 lines]
way to adapt it to search email in the Inbox and subfolders. Can anyone help
me with this? Thanks.
 
G

Guest

If the item is expected anywhere within the subfolders of the passed folder,
then it will be found. You can read objItem.Subject before explicitly
setting it to objMailItem to see if it matches.

Alternately, you can use the AdvancedSearch method which users fewer lines
of code to set a scope with sub-folder searching enabled.

--
Eric Legault (Outlook MVP, MCDBA, MCTS: Messaging & Collaboration)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/


Fox via OfficeKB.com said:
I think I understand the code, but I am only looking for one mail item that
could be anywhere. Would I insert code to check the subject after the line:
Set objMailItem = objItem

Then, if the subject I'm looking for matches, would I exit the sub? Would
the mail item still be selected that I would copy the information from or do
I need to copy the information while still in this sub?
This procedure is generic enough; it will loop through all e-mails in the
passed folder, as well as within all subfolders underneath. Just call it
once with your parent folder:

e.g.
ParseSubFolders MyParentFolder

Sub ParseSubFolders(objCurrentFolder As Outlook.MAPIFolder)
Dim objItems As Outlook.Items
Dim objItem As Object, objMailItem As Outlook.MailItem

'FIND ALL E-MAIL MESSAGES IN THE CURRENT FOLDER
Set objItems = objCurrentFolder.Items
For Each objItem In objItems
If objItem.Class = olMail Then
Set objMailItem = objItem
End If
Next

For Each objCurrentFolder In objCurrentFolder.Folders
If objCurrentFolder.DefaultItemType = olMailItem Then
ParseSubFolders objCurrentFolder
End If
Next

End Sub
I have an Excel macro already written to loop through an Outlook folder
(Inbox or a subfolder I provide) and find and email based on the subject. If
[quoted text clipped - 11 lines]
way to adapt it to search email in the Inbox and subfolders. Can anyone help
me with this? Thanks.
 
F

Fox via OfficeKB.com

Ok, this is what I came up with based on you code:

Private Function ParseSubFolders(olCurrentFolder As Object, strMailSubject As
String) As Variant
Dim olMailMsg As Variant

For Each olMailMsg In olCurrentFolder.Items
If olMailMsg.Class = 43 And InStr(olMailMsg.Subject, strMailSubject)
ParseSubFolders = olMailMsg
Exit Function
End If
Next

For Each olCurrentFolder In olCurrentFolder.Folders
If olCurrentFolder.DefaultItemType = 0 Then
ParseSubFolders olCurrentFolder, strMailSubject
End If
Next
End Function

But it seems that after I set ParseSubFolders = olMailMsg, the Exit Function
only exits at the particular level the function is at, and ParseSubFolders
becomes empty. Is there any way to retain ParseSubFolders as the functions
exit out of themselves?
If the item is expected anywhere within the subfolders of the passed folder,
then it will be found. You can read objItem.Subject before explicitly
setting it to objMailItem to see if it matches.

Alternately, you can use the AdvancedSearch method which users fewer lines
of code to set a scope with sub-folder searching enabled.
I think I understand the code, but I am only looking for one mail item that
could be anywhere. Would I insert code to check the subject after the line:
[quoted text clipped - 36 lines]
 
G

Guest

Try the variation below and this should work for you:

Sub ParseSubFolders(objCurrentFolder As Outlook.MAPIFolder,
SubjectSearchCriteria As String)
Dim objItems As Outlook.Items
Dim objItem As Object, objMailItem As Outlook.MailItem

'FIND ALL E-MAIL MESSAGES IN THE CURRENT FOLDER
Set objItems = objCurrentFolder.Items
For Each objItem In objItems
If objItem.Class = olMail Then
If InStr(objItem.Subject, SubjectSearchCriteria) > 0 Then
Set objMailItem = objItem
'DO WHAT YOU NEED TO DO HERE
'If you don't need to loop any more for this folder, call
Exit Sub
'If you need to abandon ALL looping, set a value to a
Static variable and evaluate it at the beginning and exit if you need to;
this will handle exiting a recursion
End If
End If
Next

For Each objCurrentFolder In objCurrentFolder.Folders
If objCurrentFolder.DefaultItemType = olMailItem Then
ParseSubFolders objCurrentFolder
End If
Next

End Sub

--
Eric Legault (Outlook MVP, MCDBA, MCTS: Messaging & Collaboration)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/


Fox via OfficeKB.com said:
Ok, this is what I came up with based on you code:

Private Function ParseSubFolders(olCurrentFolder As Object, strMailSubject As
String) As Variant
Dim olMailMsg As Variant

For Each olMailMsg In olCurrentFolder.Items
If olMailMsg.Class = 43 And InStr(olMailMsg.Subject, strMailSubject)
ParseSubFolders = olMailMsg
Exit Function
End If
Next

For Each olCurrentFolder In olCurrentFolder.Folders
If olCurrentFolder.DefaultItemType = 0 Then
ParseSubFolders olCurrentFolder, strMailSubject
End If
Next
End Function

But it seems that after I set ParseSubFolders = olMailMsg, the Exit Function
only exits at the particular level the function is at, and ParseSubFolders
becomes empty. Is there any way to retain ParseSubFolders as the functions
exit out of themselves?
If the item is expected anywhere within the subfolders of the passed folder,
then it will be found. You can read objItem.Subject before explicitly
setting it to objMailItem to see if it matches.

Alternately, you can use the AdvancedSearch method which users fewer lines
of code to set a scope with sub-folder searching enabled.
I think I understand the code, but I am only looking for one mail item that
could be anywhere. Would I insert code to check the subject after the line:
[quoted text clipped - 36 lines]
way to adapt it to search email in the Inbox and subfolders. Can anyone help
me with this? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top