Iterate all email items in a specific Outlook folder

How can I sort out all the email items in a specific Outlook folder in the Outlook VBA macro (in this case, the folder does not belong to my personal inbux, but is a subfolder for the inbox of the public mailbox.

Something like this, but I never did an Outlook macro ...

For each email item in mailboxX.inbox.mySubfolder.items // do this next item 

I tried this, but the subfolder was not found ...

 Private Sub Application_Startup() Dim objNS As Outlook.NameSpace Dim objFolder As Outlook.MAPIFolder Set objNS = GetNamespace("MAPI") Set objFolder = objNS.Folders("myGroupMailbox") Set objFolder = objFolder.Folders("Inbox\mySubFolder1\mySubFolder2") On Error GoTo ErrorHandler Dim Msg As Outlook.MailItem For Each Item In objFolder.Items If TypeName(Item) = "MailItem" Then Set Msg = Item If new_msg.Subject Like "*myString*" Then strBody = myItem.Body Dim filePath As String filePath = "C:\myFolder\test.txt" Open filePath For Output As #2 Write #2, strBody Close #2 End If End If ProgramExit: Exit Sub ErrorHandler: MsgBox Err.Number & " - " & Err.Description Resume ProgramExit Next Item End Sub 
+6
source share
3 answers

Format:

 Set objFolder = objFolder.Folders("Inbox").Folders("mySubFolder1").Folders("mySubFolder2") 

As stated in the comment "move the next line of position to the label ProgramExit"

+3
source

In my case, it worked:

 Sub ListMailsInFolder() Dim objNS As Outlook.NameSpace Dim objFolder As Outlook.MAPIFolder Set objNS = GetNamespace("MAPI") Set objFolder = objNS.Folders.GetFirst ' folders of your current account Set objFolder = objFolder.Folders("Foldername").Folders("Subfoldername") For Each Item In objFolder.Items If TypeName(Item) = "MailItem" Then ' ... do stuff here ... Debug.Print Item.ConversationTopic End If Next End Sub 

Similarly, you can also iterate over calendar objects:

 Private Sub ListCalendarItems() Set olApp = CreateObject("Outlook.Application") Set olNS = olApp.GetNamespace("MAPI") Set olRecItems = olNS.GetDefaultFolder(olFolderTasks) strFilter = "[DueDate] > '1/15/2009'" Set olFilterRecItems = olRecItems.Items.Restrict(strFilter) For Each Item In olFilterRecItems If TypeName(Item) = "TaskItem" Then Debug.Print Item.ConversationTopic End If Next End Sub 

Note that this example uses filtering as well as .GetDefaultFolder(olFolderTasks) to get the built-in folder for calendar items. If you want to access your inbox, for example, use olFolderInbox .

+2
source
 Sub TheSub() Dim objNS As Outlook.NameSpace Dim fldrImAfter As Outlook.Folder Dim Message As Outlook.MailItem 'This gets a handle on your mailbox Set objNS = GetNamespace("MAPI") 'Calls fldrGetFolder function to return desired folder object Set fldrImAfter = fldrGetFolder("Folder Name Here", objNS.Folders) For Each Message In fldrImAfter.Items MsgBox Message.Subject Next End Sub 

A recursive function to cycle through all folders until the specified folder name is found ....

 Function fldrGetFolder( _ strFolderName As String _ , objParentFolderCollection As Outlook.Folders _ ) As Outlook.Folder Dim fldrSubFolder As Outlook.Folder For Each fldrGetFolder In objParentFolderCollection 'MsgBox fldrGetFolder.Name If fldrGetFolder.Name = strFolderName Then Exit For End If If fldrGetFolder.Folders.Count > 0 Then Set fldrSubFolder = fldrGetFolder(strFolderName, fldrGetFolder.Folders) If Not fldrSubFolder Is Nothing Then Set fldrGetFolder = fldrSubFolder Exit For End If End If Next End Function 
+2
source

All Articles