![]() |
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. |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
![]()
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
|
|||
|
|||
![]()
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
|
|||
|
|||
![]()
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
|
|||
|
|||
![]()
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
|
|||
|
|||
![]()
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
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
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 |