Is this correct use of the "Folders" object to get to the Folder Count property? RRS feed

  • Question

  • Originally I used multiple dot operators to get to the Count property... like this
     If objFolder.Folders.Count > 0 Then bla bla
    To avoid this I did the following...
     Dim objThisfolder = Folders
     Set objThisfolder = objFolder.Folders
     If objThisFolder.Count > 0 then bla bla
    See the full example below
    Is there a better way to do this?
    Sub SubFolderCount() 
    Dim myOlApp As Outlook.Application
    Set myOlApp = Outlook.Application
    Dim objNS As NameSpace
    Set objNS = myOlApp.GetNamespace("MAPI")
    Dim objFolders As MAPIFolder
    Set objFolders = objNS.Folders(1)            'fyi - I'm restricting this to folders in "My Outlook Folders"
    Dim objFolder As MAPIFolder
    '-- I declared this...
    Dim objThisFolder As Folders
    For Each objFolder In objFolders.Folders
      '-- to create this object...
        Set objThisFolder = objFolder.Folders  '
     '-- to access this Property & avoid multiple dot operators
        If objThisFolder.Count > 0 Then
            Debug.Print objFolder.Name;
            Debug.Print objThisFolder.Count
        End If
    End Sub
    Thanks for any help.

    • Edited by Mel_3 Monday, January 9, 2012 10:24 PM
    Monday, January 9, 2012 10:14 PM

All replies

  • Are you just trying to make sure your code is efficient? Or actually trying to solve a particular problem?

    Are you trying to recursively count the number of child folders?

    Dmitry Streblechenko (MVP)
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Monday, January 9, 2012 10:18 PM
  • Hello Dmitry,

    I was just trying to come up with an alternate way to code objFolder.Folders.Count

    and eliminate the multiple dots.

     My solution was to create a new object...

    Dim objThisfolder As Folders ' declare a folders object
    Set objThisFolder = Folders.Count ' create the object
    Debug.Print objThisFolder.Count ' use the object

    The example I supplied in the OP works.

    1 - Is this a proper use of the Folders Object?
    2 - Would you do it much differently?

    Thanks again for the help.



    Monday, January 9, 2012 10:48 PM
  • If you call it only once, multiple dot notation is fine.

    If you do that multiple times, e.g. in a loop, I would split the statement into multiple statements.

    Dmitry Streblechenko (MVP)
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Wednesday, January 11, 2012 2:25 PM