0

Recently I am trying to create a function which selects specific folders.

I tested this at one user and it works. The problem however is that I also want to use this function at other users but not having to rewrite the function based on their folder structure.

The function works with a few folder names which all of them have (same names). It consists of 1 main folder: #MemoScan and 4 sub-folders.

Based on these folder names I want to count how many mail items are in them.

I created the following function to do this:

Function HowManyEmails() As Integer

    Dim objOutlook As Object, objnSpace As Object, MyCurrentFolder As MAPIFolder
    Dim EmailCount As Integer
    Set objOutlook = CreateObject("Outlook.Application")
    Set objnSpace = objOutlook.GetNamespace("MAPI")

    Set MyCurrentFolder = objnSpace.folders("William").folders("#MemoScan")

    sFolder = MyCurrentFolder

    For Each Folder In MyCurrentFolder.folders
        sFolder = Folder
        sSubmap = Right(sFolder, Len(sFolder) - 1)

        For Each Item In Folder.Items

            If TypeName(Item) = "MailItem" Then

                 EmailCount = EmailCount + 1
            End If
        Next Item
    Next Folder

    HowManyEmails = EmailCount

End Function  

As you can see the folder that needs to be checked is hard-coded (needs to be since it runs on a close outlook event and nothing is selected). The path now is: objnSpace.folders("William").folders("#MemoScan")

The thing is however that the main account/folder William wont be there at other users. My question is, how can I adjust it in such way that it will just look for the #MemoScan folder which is of the same at every user? Is this even possible?

If I leave the main William namespace out then it won't be able to find the #MemoScan folder.

The folder structure at this particular user is as follows:

enter image description here

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Nicolas
  • 2,277
  • 5
  • 36
  • 82
  • 1
    maybe this tutorial will be useful http://stackoverflow.com/questions/8697493/update-excel-sheet-based-on-outlook-mail/8699250#8699250 – Dawid May 26 '15 at 10:24

1 Answers1

2

The Namespace class provides the Stores property which returns a Stores collection object that represents all the Store objects in the current profile. The Store class provides the GetRootFolder method which returns a Folder object representing the root-level folder of the Store. You can use the GetRootFolder method to enumerate the subfolders of the root folder of the Store. Unlike NameSpace.Folders which contains all folders for all stores in the current profile, Store.GetRootFolder.Folders allows you to enumerate all folders for a given Store object in the current profile.

 Sub EnumerateFoldersInStores() 
  Dim colStores As Outlook.Stores 
  Dim oStore As Outlook.Store 
  Dim oRoot As Outlook.Folder 
  On Error Resume Next 
  Set colStores = Application.Session.Stores 
  For Each oStore In colStores 
   Set oRoot = oStore.GetRootFolder 
   Debug.Print (oRoot.FolderPath) 
   EnumerateFolders oRoot 
  Next 
 End Sub 

 Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder) 
  Dim folders As Outlook.folders 
  Dim Folder As Outlook.Folder 
  Dim foldercount As Integer 
  On Error Resume Next 
  Set folders = oFolder.folders 
  foldercount = folders.Count 
  'Check if there are any folders below oFolder 
  If foldercount Then 
   For Each Folder In folders 
    Debug.Print (Folder.FolderPath) 
    EnumerateFolders Folder 
   Next 
  End If 
 End Sub

Also you can run the code against the currently selected folder in Outlook. The CurrentFolder property of the Explorer class returns a Folder object that represents the current folder displayed in the explorer.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45