Xavi
Xavi

Reputation: 207

How to access an Outlook folder from an Excel macro

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 [email protected] 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

Upvotes: 0

Views: 1978

Answers (2)

Xavi
Xavi

Reputation: 207

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

Upvotes: 0

Tragamor
Tragamor

Reputation: 3634

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?

Upvotes: 2

Related Questions