Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on 

Outlook Email Folder Names

At:

https://www.experts-exchange.com/questions/23567902/Audit-Outlook-Folders.html

I have a good solution from Rick_Rickards, I like to revise it to read the folder name that have or can have emails (not interested in folders like calendar or Journal, etc.). I want to read these folder names to populate a table in access (later to populate a combo box for user to pick a folder ....).

The existing code is included below.

Question: How can I revise it so that it will read the name of folder created to contain email messages?

Thank you,

Mike

Sub Sample()
    Dim outApp As New Outlook.Application
    Dim outNS As Outlook.NameSpace
    Dim outFdr  As Outlook.MAPIFolder
    Set outNS = outApp.GetNamespace("MAPI")
    For Each outFdr In outNS.Folders
        OMF2 outFdr
    Next outFdr
End Sub
 
Function OMF2(F As Outlook.MAPIFolder, Optional lngLayer As Long = 0)
    Dim lng As Long
    'Debug.Print Space(lngLayer * 3) & f.Name
    CurrentDb.Execute "Insert Into tOLK_Folders(Folder,Class,CurrentView,Description,FolderPath) Values('" & F.Name & "', '" & CStr(F.Class) & "', '" & CStr(F.CurrentView) & "', '" & F.Description & "', '" & F.FolderPath & "')"
    For lng = 1 To F.Folders.Count
        OMF2 F.Folders(lng), lngLayer + 1
    Next lng
End Function

Open in new window

Microsoft AccessOutlookVisual Basic Classic

Avatar of undefined
Last Comment
Rick_Rickards
ASKER CERTIFIED SOLUTION
Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

The solution probably merits some explanation.  Outlook doesn't discriminate folders by type but that doesn't mean you couldn't do what you were trying to do.  It does discriminate each folder based on the default item type (meaning when you create an item within that folder what type of item gets created).  Mail Items are type 0 and thus any Folder with a DefaultItemType of 0 is a mail folder.  As you might have guessed each kind of item you can create within a folder is identified by a number (type).

The above example overlooks the fact that the Top Folder (When lngLayer = 0) is technically not a Mail Folder per se.  It can have folders of any type created underneath it and outlook will tell you the default type is 0, which is true.  Hence, when we're looking at the top of the tree it needs to be understood that that is not a mail folder but rather the highest branch in your outlook tree.  Since outlook will now avail multiple .pst files to you at once, if you didn't hae this information you'd lack any way to know which PST file the information was coming from.

Hence the caveat in my previous comment.  The good news is that the solution you now seek vs. the one orginaly posted requires little more than a single line of code to be added (Line 13).  Basically it checks to see if you're at the top of the tree (Layer 0) and if so allows the display of the folder not withstanding the issues previously mentioned.  All other branches are discriminated, if the default item type is not 0 (a mail item) then it exist the function and does not explore that branch any further.
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

That was quick. I will try it after lunch.

Thanks,

Mike
Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

Have fun!!!
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

I had to add a second line:

    If lngLayer > 0 Then If F.DefaultItemType <> 0 Then Exit Function
    If InStr(F.Name, "All Public Folders") > 0 Then Exit Function

to bypass the public folders (from all other office nationwide).

brb
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

If InStr(F.Name, "All Public Folders") > 0 Then Exit Function

wasn't necessary. I was doing something wrong I guess.
Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

No problem.  So is it working for you?
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

Thank you.
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

My next question will be about folder constants I will be talking about.
                                   folder constants ---------v
Set obj_inbox = obj_ns.GetDefaultFolder(olFolderInbox)

Where can I get them for each folder like:

For "\\Mailbox - Eghtebas, Mike\Inbox" I use "olFolderInbox"

What it would be for: "\\Mailbox - Eghtebas, Mike\Sent Items"?

I am posting a new question on this one.

Mike

Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

I'll watch for it.  You'll notice at the bottom of the screen it has a link, "As a related question".  When I return I'll see if its there.
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo