0

I have VBA code in Excel to select the main Outlook inbox. I would like to select any folder or subfolder in that inbox.

For example, I would like to select the subfolder ALD in this screenshot of my main inbox:

enter image description here

I have another email address in Outlook with folders and subfolders. I would like to select any folder or subfolder of this other email address. For example, I have another email address called xxxx@yyyy.com and a folder aaaa and inside a subfolder bbbb. How would I select the subfolder bbbb?

Sub OpenOutlookFolder()
    Dim xOutlookApp As Outlook.Application
    Dim xNameSpace As Outlook.Namespace
    Dim xFolder As Outlook.Folder
    Dim xFolderType As OlDefaultFolders
    On Error Resume Next
    
    Set xOutlookApp = New Outlook.Application
    Set xNameSpace = xOutlookApp.Session
    Set xFolder = xNameSpace.GetDefaultFolder(olFolderInbox
    xFolder.Display
    Set xFolder = Nothing
    Set xNameSpace = Nothing
    Set xOutlookApp = Nothing
    Exit Sub
End Sub
Robson
  • 2,008
  • 2
  • 7
  • 26
Xavi
  • 207
  • 1
  • 3
  • 20
  • [this question](https://stackoverflow.com/questions/43613459/select-outlook-folder-with-excel-vba) doesn't have a verified answer but it will help you. See Vityaya's answer. – Scott Holtzman Jan 06 '21 at 14:31
  • Possible duplicate of [Get reference to additional Inbox](https://stackoverflow.com/questions/9076634/get-reference-to-additional-inbox) – niton Jan 06 '21 at 14:40
  • thanks a lot to all of you for your answers! Unfortunately it does not work, I mean I do not manage to modify my macro in order that it performs what I want, I tried all the solutions suggested in the topic "Get reference to additional inbox"... Even if I have good VBA knowledges, I am not a developper so it's hard for me to see exactly which modifications I have to bring in order that my macro runs as I would like it does .. Anyway it gives me ideas... – Xavi Jan 06 '21 at 21:00
  • Incorrect suggested duplicate. See here https://stackoverflow.com/questions/8322432/using-visual-basic-to-access-subfolder-in-inbox – niton Jan 07 '21 at 14:41

2 Answers2

2

Something along the lines of:

Dim ThisNamespace As Outlook.NameSpace: Set ThisNamespace = Application.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder: Set Inbox = ThisNamespace.GetDefaultFolder(olFolderInbox)

Dim BaseFolder As Outlook.MAPIFolder: Set BaseFolder = Inbox '.Folders("SubFolder1\SubFolder2...")

For direct subfolder access, uncomment within the last line and update the path

If you want to create a folder structure which is searchable/editable then my answer in this question may be of interest: How can one iterate through the subfolders of a subfolder of a shared mail inbox folder?

Tragamor
  • 3,594
  • 3
  • 15
  • 32
0

Finally by checking again the link Get reference to additional Inbox, I managed to modify my macro in order it works and does as I want. Please find the code below:

Sub OpenOutlookFolderworks()

Dim xOutlookApp As Outlook.Application

Dim xNameSpace As Outlook.Namespace
Dim xFolder As Outlook.Folder
 Dim vRecipient As Outlook.MAPIFolder
 Dim xFolderType As OlDefaultFolders
On Error Resume Next
Set xOutlookApp = New Outlook.Application
Set xNameSpace = xOutlookApp.Session
Set xFolder = xNameSpace.GetDefaultFolder(olFolderInbox)
Set xFolder = xFolder.Folders("payment office")
Set xFolder = xFolder.Folders("JIRA")
xFolder.display
Set xFolder = Nothing
Set xNameSpace = Nothing
Exit Sub
End Sub
Xavi
  • 207
  • 1
  • 3
  • 20