A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Search for email in Inbox and subfolders



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old May 1st 06, 05:15 PM posted to microsoft.public.outlook.program_vba
Fox via OfficeKB.com
external usenet poster
 
Posts: 5
Default Search for email in Inbox and subfolders

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.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...g-vba/200605/1
  #2  
Old May 2nd 06, 10:18 PM posted to microsoft.public.outlook.program_vba
Eric Legault [MVP - Outlook]
external usenet poster
 
Posts: 830
Default Search for email in Inbox and subfolders

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

--
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" wrote:

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.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...g-vba/200605/1

  #3  
Old May 3rd 06, 09:41 PM posted to microsoft.public.outlook.program_vba
Fox via OfficeKB.com
external usenet poster
 
Posts: 5
Default Search for email in Inbox and subfolders

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?

Eric Legault [MVP - Outlook] wrote:
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.


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...g-vba/200605/1
  #4  
Old May 3rd 06, 10:51 PM posted to microsoft.public.outlook.program_vba
Eric Legault [MVP - Outlook]
external usenet poster
 
Posts: 830
Default Search for email in Inbox and subfolders

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" wrote:

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?

Eric Legault [MVP - Outlook] wrote:
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.


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...g-vba/200605/1

  #5  
Old May 5th 06, 05:13 PM posted to microsoft.public.outlook.program_vba
Fox via OfficeKB.com
external usenet poster
 
Posts: 5
Default Search for email in Inbox and subfolders

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)
0 Then

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?

Eric Legault [MVP - Outlook] wrote:
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.


--
Message posted via http://www.officekb.com
  #6  
Old May 8th 06, 04:35 PM posted to microsoft.public.outlook.program_vba
Eric Legault [MVP - Outlook]
external usenet poster
 
Posts: 830
Default Search for email in Inbox and subfolders

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" wrote:

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)
0 Then

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?

Eric Legault [MVP - Outlook] wrote:
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.


--
Message posted via http://www.officekb.com

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search Email Headers? [email protected] Outlook - General Queries 2 May 1st 06 05:24 AM
Inbox subfolders used to pop up reminders, not in this new contract w/O2K (nor at home in new O2003). StargateFanFromWork Outlook - General Queries 5 March 28th 06 04:26 PM
Search all subfolders? Chris Lovett Outlook - General Queries 1 March 20th 06 06:33 PM
email shot/ search results Noha Diab Outlook - Using Contacts 2 February 14th 06 04:18 PM
Outlook Rules: when email comes into inbox want to place email address into contact list [email protected] Outlook - General Queries 4 January 13th 06 08:01 AM


All times are GMT +1. The time now is 12:53 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.