VBA Code to Browse an Outlook Folder


Outlook is most commonly used emailing application used in the world. Many people spend their entire day on Outlook application to read and response to emails. To automate certain rule based tasks in Outlook, we can take help of VBA. Below is one of the commonly used VBA code to browse an Outlook folder. The code returns two values:

(i) Outlook Folder Path
(ii) EntryID

Outlook folder path can be used to display the selected folder path to the user and EntryID can be used contact with Outlook and read emails from the folder.

Excel Tricks

You can also read my post for reading Outlook emails in Excel

Public Sub PickOutlookFolder()
    'Microsoft Outlook XX.X Object Library is required to run this code
    'Variable declaration
    Dim objNS As Namespace
    Dim objFolder As Folder
    Dim strFolderPath As String
    Dim strEntryID As String
    'Set Outlook Object
    Set objNS = Outlook.GetNamespace("MAPI")
    Set objFolder = objNS.PickFolder
    If TypeName(objFolder) <> "Nothing" Then
        strFolderPath = objFolder.FolderPath
        strEntryID = objFolder.EntryID
    End If
    'Close the objects
    Set objFolder = Nothing
    Set objNS = Nothing
    'Show the selected folder details on Excel sheet
    Sheet1.Range("B6").Value = strFolderPath
    Sheet1.Range("C6").Value = strEntryID
End Sub

It is worth to mention that you need to add Outlook reference (Microsoft Outlook XX.X Object Library) in Excel VBA from Menu Bar (Tools>References…). Below are the steps to add Outlook reference in Excel VBA:

1. From the Menu Bar, click on Tools > References…

Excel Tricks

2. Select ‘Microsoft Outlook XX.X Object Library’ and click on ‘OK’ button

Excel Tricks

To use this code in your Excel file, follow below steps:

1. Open an Excel file
2. Press Alt+F11
3. Insert a Module (Insert>Module) from menu bar
4. Paste the code in the module
5. Now add a shape in Excel sheet
6. Give a name to the shape like ‘Browse Outlook Folder’

Excel Tricks

7. Right click on the shape and select ‘Assign Macro…’

Excel Tricks

8. Select PickOutlookFolder from the list and click on ‘Ok’ button

Excel Tricks

9. Done

Leave a Reply

avatar
VBA Code to Delete File
EXCEL VBA TRICKS
VBA Code to Delete File

In this article we are going to show you how you can delete file or files using a single line of VBA code. 1. Delete a specific file from the folder, 2. Delete specific type of files from the folder, 3. Delete all files from the folder

VBA Code to Add Border to Excel Range
EXCEL VBA TRICKS
VBA Code to Add Border to Excel Range

Through formatting the cells, you can make your Excel data more beautiful and easier to understand. If you record a macro to add borders to Excel range, you will notice that it generates 30 plus lines of code for just a small work. Here we are sharing one line of code which does the same thing.

Protect Excel Sheet for Manual Input
EXCEL VBA TRICKS
VBA Code to Protect Excel Sheet for Manual Input but Allow Programming Inputs

Did you come across any requirement where you want the user to interact with a sheet only through VBA Form? Here is a simple code which can help you.