0

I am trying to access an Outlook folder with Excel VBA.
Some folders I am able to access but a few folders are not accessible.

I am able to read one subfolder with the below code.
When I try to change the folder it is giving the error.

Public Sub ReadOutlookEmails()
    Dim out_app As Outlook.Application
    Dim get_name As Outlook.Namespace
    Dim get_folder As Outlook.MAPIFolder
    Dim oAccount As Object
    Dim store_add As Object
    
    Dim monthKeyValuePair As New Scripting.Dictionary
    
    Dim email_list As New mscorlib.ArrayList
    Dim date_List As New mscorlib.ArrayList
    
    For Each c In Worksheets(ActiveSheet.Name).Range("D8:AH8")
        date_List.Add c
        'MsgBox c
    Next c
    
    Set out_app = New Outlook.Application
    
    Set get_name = out_app.GetNamespace("MAPI")
    
    For Each oAccount In out_app.Session.Accounts
     
     If oAccount.SmtpAddress = "[email protected]" Then
      
        Set store_add = oAccount.DeliveryStore
        
        'MsgBox store_add.GetDefaultFolder(olFolderInbox).Folders("New Joinees")
        
        'Set get_folder = store_add.GetDefaultFolder(olFolderInbox).Folders("On Bench Training")
        
        Set get_folder = store_add.GetDefaultFolder(olFolderInbox)
        Set get_folder = get_folder.Folders("On Bench Training")

enter image description here

2 Answers 2

1

The error is MAPI_E_NOT_FOUND, which means the folder with the given name does not exist.

Make sure the folder named "On Bench Training" is really a subfolder of the Inbox.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it is there
Can you see it in Outlook in the folder tree?
0

Instead of getting the folder by its name you may try iterating over all subfolders and checking their name. So, basically instead of the following line:

Set get_folder = get_folder.Folders("On Bench Training")

You may iterate over all subfolder:

For Each uFolder In get_folder.Folders
  If uFolder.Name = "On Bench Training" Then
        MsgBox "Found!"
  End If
Next uFolder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.