VBA to loop through all inboxes including shared inboxes

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



VBA to loop through all inboxes including shared inboxes



I have working code that replies to an email in the user's Outlook, based on the subject. However I am not able to have the code search through all the user's inboxes.



As of now it will only search through the user's specific inbox. Here is my code, I have searched around but I can not find a solution that my knowledge of VBA can comprehend.


Sub Display()

Dim Fldr As Outlook.Folder
Dim olfolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olReply As Outlook.MailItem
Dim olItems As Outlook.Items
Dim i As Integer
Dim signature As String

Set Fldr = Session.GetDefaultFolder(olFolderInbox)
Set olItems = Fldr.Items

olItems.Sort "[Received]", True

For i = 1 To olItems.count
signature = Environ("appdata") & "MicrosoftSignatures"

If Dir(signature, vbDirectory) <> vbNullString Then
signature = signature & Dir$(signature & "*.htm")
Else
signature = ""
End If

signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll

Set olMail = olItems(i)

If InStr(olMail.Subject, Worksheets("Checklist Form").Range("B8")) <> 0 Then
If Not olMail.Categories = "Executed" Then
Set olReply = olMail.ReplyAll

With olReply
.HTMLBody = "<p style='font-family:calibri;font-size:14.5'>" & "Hi Everyone," & _
"<p style='font-family:calibri;font-size:14.5'>" & "Workflow ID:" & " " & _
Worksheets("Checklist Form").Range("B6") & "<p style='font-family:calibri;font-size:14.5'>" & _
Worksheets("Checklist Form").Range("B11") & "<p style='font-family:calibri;font-size:14.5'>" & _
"Regards," & "</p><br>" & signature & .HTMLBody
.Display
.Subject = "RO Finalized WF:" & Worksheets("Checklist Form").Range("B6") & " " & _
Worksheets("Checklist Form").Range("B2") & " -" & Worksheets("Fulfillment Checklist").Range("B3")
End With

Exit For
olMail.Categories = "Executed"

End If
End If

Next i

End Sub





You should just be able to add another for loop right after the Set Fldr... line ` For Each mSubfolder In Fldr.Folders` and lastly you'd have to change the line after it to Set olItems = mySubfolder.Items
– Marcucciboy2
Aug 9 at 17:32



Set Fldr...


Set olItems = mySubfolder.Items





if that doesn't work check out this answer stackoverflow.com/a/2273050/2727437
– Marcucciboy2
Aug 9 at 17:34





Is it supposed to be mSubfolder? or mysubfolder and also do I need to declare it?
– Tmacjoshua
Aug 9 at 18:36





oops sorry about that typo there. I meant for them both to be "my". mySubfolder was just an example name for a folder object, so it'd be Dim mySubfolder As Outlook.Folder
– Marcucciboy2
Aug 9 at 18:37



mySubfolder


Dim mySubfolder As Outlook.Folder





I can not seem to get it to work. Would you mind answering the question with my code and the code lines needed attached. I must be missing something. Thanks Marc
– Tmacjoshua
Aug 9 at 18:49




2 Answers
2



You may reference any Inbox like this:


Option Explicit

Sub Inbox_by_Store()

Dim allStores As Stores
Dim storeInbox As Folder

Dim j As Long

Set allStores = Session.Stores

For j = 1 To allStores.count

Debug.Print j & " DisplayName - " & allStores(j).DisplayName

Set storeInbox = Nothing

' Some stores will not have an inbox
' Bypass possible expected error if there is no inbox in the store
On Error Resume Next
' Note this is one of the rare acceptable uses for On Error Resume Next
Set storeInbox = allStores(j).GetDefaultFolder(olFolderInbox)
' Turn off error bypass as soon as it is no longer needed
On Error GoTo 0

If Not storeInbox Is Nothing Then
storeInbox.Display

' your code here instead of storeInbox.Display
' Set Fldr = storeInbox

End If

Next

ExitRoutine:
Set allStores = Nothing
Set storeInbox = Nothing

End Sub





Thanks Niton, sorry but where would I incorporate that in my code. Will this code read all inboxes?
– Tmacjoshua
Aug 10 at 14:44





I am not sure how to input more than one "For i = 1" Is this possible?
– Tmacjoshua
Aug 10 at 17:09






Change this outer loop to something else, perhaps j.
– niton
Aug 10 at 17:13





Put your code with one change Set Fldr = storeInbox where indicated.
– niton
Aug 10 at 17:45


Set Fldr = storeInbox





Any Debug.Print is not necessary to run code. Without knowing why there is an error, you can place on error resume next just before and on error goto 0 immediately after. It may still be useful for debugging purposes, if it does not produce helpful output delete it.
– niton
Aug 13 at 13:05


on error resume next


on error goto 0



I don't really have the ability to test out whether this works, but these are the changes that I mentioned in the comments, I hope they work!


Sub Display()

'...

Set Fldr = Session.GetDefaultFolder(olFolderInbox)

Dim mySubfolder As Outlook.Folder 'added
For Each mySubfolder In Fldr.Folders 'added

Set olItems = mySubfolder.Items 'changed

For i = 1 To olItems.count

'...

Next i

Next mySubfolder 'added

End Sub





I get the error (Object Required) related to code line "Set olitems =myFldr.Folders"
– Tmacjoshua
Aug 9 at 19:04





@Tmacjoshua oops, yeah it was supposed to be Set olItems = mySubfolder.Items
– Marcucciboy2
Aug 9 at 19:06


Set olItems = mySubfolder.Items





When I input a subject, to find an email nothing happened. When I did not input a subject about 7 random emails showed up from different times, when it is supposed to sort by the most recent email.
– Tmacjoshua
Aug 9 at 19:12





Ah well I'm not totally sure how to solve that, then :/ It seems like you might have to spend a bit of time using F8 and the locals window to debug and see where everything is going excel-easy.com/vba/examples/debugging.html
– Marcucciboy2
Aug 9 at 19:17



F8


locals window






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard