• Email folders & subfolders? (2002)

    Author
    Topic
    #372614

    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

    Set myFolder = myNamespace.GetDefaultFolder(olFolderSentMail)

    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.

    Viewing 0 reply threads
    Author
    Replies
    • #596199

      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!

      • #596227

        HOW do i select each folder item? I am lost in the object hierarchy, and there doesnt seem to be a

        for each myfolder.subfolder

        or equivalent, so I see no way brickwall to recurse thru the folder tree.

        • #596340

          Try this:

          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

          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!!

          • #596376

            EGG zackly what i was looking for.

            Thanks, Jefferson bow

          • #596760

            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?

            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
            
            • #596986

              *sigh* I got bit by vba.

              The line should be either

              call ListSubfolders (ofolder)

              or

              ListSubfolders ofolder

              aflame

            • #597435

              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.

            • #633668

              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 below.

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

            • #633720

              > (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. grin

            • #633877

              [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.

            • #636695

              Ohh, ahh, also works for Outlook 2000/Win XP:

            • #983038

              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… brickwall

              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

            • #983039

              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.

            • #983057

              Hans – thanks! That’s quite amazing that you can call a procedure on itself – but it certainly works.

            • #1108518

              Hi, Hans – I need this code again, but the link does not work – any thoughts?

            • #1108519

              Like many others, this attachment was lost in the server crash of August 2007. I don’t have the code any more either, but I’ll see if the old brain cells can reconstruct it later today, I don’t have enough time now.

            • #1108520

              Ahh.

              No probs, I am too trying to reconstruct the code. Thanks Hans.

            • #1108521

              Ok, here is the code I have currently.

              It is to duplicate the folder structure of a .pst file. it copies the structure of a pst called ‘Current’, to a empty .pst called ‘temp’

              this is helpful as many managers want to copy their pst structure, and i can run this code to provide them with an empty structure.

              Sub r1()
              s1 Session.Folders(“Current”), Session.Folders(“temp”)
              End Sub

              Sub s1(fRoot As MAPIFolder, fCopy As MAPIFolder)
              On Error GoTo 1
              Dim f As MAPIFolder, i As Integer, fC As MAPIFolder
              For Each f In fRoot.Folders
              Set fC = fCopy.Folders.Add(f.Name)
              If f.Folders.Count > 0 Then
              s1 f, fC
              End If
              Next
              Set f = Nothing
              Exit Sub
              1:
              Debug.Print f.Name, f.FolderPath
              Resume Next
              End Sub

            • #1108559

              Check out Download details: Outlook 2007/2003/2002 Add-in: Personal Folders Backup. It just makes a copy of the specified PST file. You can then copy it, move it, rename it.

              Joe

              --Joe

    Viewing 0 reply threads
    Reply To: Email folders & subfolders? (2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: