Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Email folders & subfolders? (2002)

    I am trying to programmatically read all the "email folders" in any OL archive.

    Working with my own Personal Folders, I can open any of the "main" or "default" folders using <pre>Set myFolder = myNamespace.GetDefaultFolder(olFolderSentMail)</pre>

    My own Inbox has several subfolders in which i keep incoming and responses. What is the VBA code to navigate thru ALL the folders in the archive and determine which ones have mail in them? i.e., i dont want to look thru the calendar or task folders for emails, and i dont want to miss pertinent folders either.

  2. #2
    Lounger
    Join Date
    Feb 2001
    Location
    London UK, Gtr London, England
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Email folders & subfolders? (2002)

    You will have to select each folder item in the folders collection of your Myfolders object after the line you have there.
    Search for mail items in each of those and away you go!

  3. #3
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Email folders & subfolders? (2002)

    HOW do i select each folder item? I am lost in the object hierarchy, and there doesnt seem to be a <pre>for each myfolder.subfolder</pre>

    or equivalent, so I see no way <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15> to recurse thru the folder tree.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Email folders & subfolders? (2002)

    Try this:
    <pre>Sub EnumInboxSubFolders()
    Dim ns As NameSpace, fldr As MAPIFolder
    Set ns = Application.GetNamespace("MAPI")
    Set fldr = ns.GetDefaultFolder(olFolderInbox)
    If fldr.Folders.Count > 0 Then
    Debug.Print fldr.Name & " has " & fldr.Folders.Count & " subfolder(s) named:"
    Dim ofolder As MAPIFolder
    For Each ofolder In fldr.Folders
    Debug.Print " " & ofolder.Name
    Next
    Set ofolder = Nothing
    Else
    Debug.Print fldr.Name & " has no subfolders."
    End If
    Set fldr = Nothing
    Set ns = Nothing
    End Sub</pre>

    Note that the fldr.Folders collection is not a collection of Folder objects, but a collection of MAPIFolder objects. Not sure whose bright idea it was to do it that way.

    Note that if you have multiple PSTs, like I do, the ns.Folders collection contains PSTs, not the folders in your "default" PST. This provides an opportunity to navigate to archival PSTs over which you might want to iterate.

    Note also that I use Outlook 2000 in Corp/Workgroup mode...and your mileage may vary!!

  5. #5
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Email folders & subfolders? (2002)

    EGG zackly what i was looking for.

    Thanks, Jefferson <img src=/S/bow.gif border=0 alt=bow width=15 height=15>

  6. #6
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Email folders & subfolders? (2002)

    One more question: when i try to pass a MAPIfolder as an argument, so i can visit sub- and sub-sub-folders, i get error 424 object expected in this code, so i wonder what i should be writing at the <<<<< or in the subroutine argument list?<pre>Sub EnumInboxSubFolders()
    ' inspired by Jefferson Scher of Woody's Lounge
    Dim ns As NameSpace
    Set ns = Application.GetNamespace("MAPI")

    Dim cnt As Integer
    cnt = ns.Folders.Count

    Dim fldr As MAPIFolder
    'Set fldr = ns.GetDefaultFolder(olFolderInbox)
    Set fldr = ns.Folders.GetFirst ' gets first archive

    Dim i As Integer
    For i = 1 To cnt ' visit all 1st level folders

    If fldr.Folders.Count > 0 Then
    Debug.Print fldr.Name & " has a total of " _
    & fldr.Folders.Count & " subfolders"

    ' visit email subfolders
    Dim ofolder As MAPIFolder
    For Each ofolder In fldr.Folders
    If ofolder.DefaultItemType = olMailItem _
    And ofolder.Name <> "Deleted Items" Then
    Debug.Print " " & ofolder.Name
    If ofolder.Folders.Count > 0 Then
    Debug.Print " with ", _
    ofolder.Folders.Count, " subfolders"

    ' what should following line be?
    ListSubfolders (ofolder) ' <<<<<

    End If
    End If
    Next
    Set ofolder = Nothing
    End If

    Set fldr = ns.Folders.GetNext ' next archive
    Next i

    Set fldr = Nothing
    Set ns = Nothing
    End Sub


    Public Sub ListSubfolders(ofolder As MAPIFolder)
    Dim sf As MAPIFolder
    For Each sf In ofolder.Folders
    Debug.Print " " & sf.Name
    Next
    End Sub
    </pre>


  7. #7
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Email folders & subfolders? (2002)

    *sigh* I got bit by vba.

    The line should be either <pre>call ListSubfolders (ofolder)</pre>

    or<pre>ListSubfolders ofolder</pre>

    <img src=/S/aflame.gif border=0 alt=aflame width=15 height=15>

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Email folders & subfolders? (2002)

    Yes, ye old "pass by reference/pass by value" problem with the parens. Many times around the block on that here. Rule of thumb: if the editor pushes that space between the procedure name and the parens, take 'em off.

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Email folders & subfolders? (2002)

    > (BTW, this code larned me that the immediates window has a display line limit.)

    In one of my rare posts in the Access Forum, I had the same revelation and posted a routine that dumped the output into a Word document. That's my hammer... I guess a real database person would have thrown it into a table. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  10. #10
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Email folders & subfolders? (2002)

    Gentlemen, thank you, thank you, this was of great help. I'm posting this adaptation for future use. Improvements welcome, as I have plenty to learn:

    Code deleted as improved version is attached to <post#=200296>post 200296</post#> below.

    (BTW, this (since deleted) code larned me that the immediates window has a display line limit.)
    -John ... I float in liquid gardens
    UTC -7ąDS

  11. #11
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Email folders & subfolders? (2002)

    Ohh, ahh, also works for Outlook 2000/Win XP:
    Attached Images Attached Images
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  12. #12
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Email folders & subfolders? (2002)

    [Edited, attached as plaintext, re-edite to remove double spacing in attachment.]
    Creates a new note listing the number of mail item folders and mail items, with immediate parent folder and subfolder count. User beware. I'm not very proficient with this stuff, all I know is, it works on my system. Probably some redundancy and unnecessary references. Testing and improvements welcome. Thanks again to Jefferson and Peter for pushing me over the hill.

    All this work and I have only a measly 13,200 odd items in my pst.
    -John ... I float in liquid gardens
    UTC -7ąDS

  13. #13
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Email folders & subfolders? (2002)

    Hi, I'm trying to do a similar thing here and am fine with the code - but have a question on the topic of iteration and subfolders...

    do we have to 'Hard-Code' for each possible subfolder or is there a way of checking each folder's count, and then 'moving down the hierarchy' automatically through all the folders using another sub? I ahve been trying to do this but my brain hurts... <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15>

    I think there should be a way of doing a 'For each folder....and if this folder has more folders then break out and do another 'For Each Folder....' etcetc ?

    I have the following code:

    Sub doAudit(fld As MAPIFolder)
    Dim myfile As Object, expOpen As Single, fso As Object
    Dim f As MAPIFolder, i() As Integer, o As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set myfile = fso.CreateTextFile("C:Temptemp.txt", True)
    For Each f In fld.Folders
    If f.Class = olFolder Then
    myfile.writeline f.Name & " - (" & f.Items.Count & ")" & " FOLDERS INSIDE - " & f.Folders.Count
    myfile.writeline "---------------------"
    'CODE HERE TO GO THROUGH SUBFOLDERS IF NECESSARY
    'MORE CODE HERE TO GO THROUGH SUBFOLDER'S SUBFOLDERS?
    'ETC ETC???
    For Each o In f.Items
    If o.Class = olMail Then
    myfile.writeline o.Subject
    End If
    Next o
    End If
    myfile.writeline vbNewLine & vbNewLine
    Next f
    myfile.Close
    Set myfile = Nothing
    Set fso = Nothing
    expOpen = Shell("Notepad C:Temptemp.txt", vbNormalFocus)
    End Sub
    Thanks,

    pmatz

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Email folders & subfolders? (2002)

    You can use recursion, i.e. let a procedure or function call itself. See the attached text file. The HandleFolder procedure calls itself for each subfolder. The process automatically stops of there are no more subfolders.

  15. #15
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Email folders & subfolders? (2002)

    Hans - thanks! That's quite amazing that you can call a procedure on itself - but it certainly works.
    Thanks,

    pmatz

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •