0

I have the following structure in my Outlook Public Folders.

-Public Folders
--1001_RandomProject
--1002_AnotherProject
--1003_Yetanotherproject
...

and so on, basically there's a couple of thousand(!) subfolders each corresponding to a project and prefixed with the project number.

Now I'm trying to create a Macro which will allow a user to select a number of mail items, enter the project number through an Inputbox and then move these mail items automatically to the subfolder corresponding to the project number.

All of this works, but in the current implementation I'm looping through all the subfolders of the Public Folders and comparing the entered project number to the first 4 characters of the folder name, to see if they match, and then move the mail items to the folder.

As you can imagine with thousands of subfolders, this tends to go quite slowly. So my question is: is there another way to select the correct folder without looping through all of them (sort of using a mask like 'find me a folder beginning with 1001*).

The simplified part of the "find the folder" code

Sub verplaatsen()

Dim Boodschap, Titel, Standaard
Dim ProjectNumber As String
Dim NrOfFolders as Integer
Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim FolderName as String


Boodschap = "Enter project number"
Titel = "Move mail"
Standaard = "0000"
ProjectNumber = InputBox(Boodschap, Titel, Standaard)

Set objOutlook = Application
Set objNamespace = objOutlook.GetNamespace("MAPI")

NrOfFolders = objNamespace.Folders(3).Folders(12).Folders.Count

I = 2

Do
    FolderName = objNamespace.Folders(3).Folders(12).Folders(I).Name
    If Left(FolderName, 4) = ProjectNumber Then
    Else
        I = I + 1
    End If
Loop Until I = NrOfFolders Or Left(FolderName, 4) = ProjectNumber

MoveMail (I)

End Sub
Sam
  • 1,514
  • 2
  • 14
  • 22
  • Could you provide your code? It's hard to say, if another macro will perform better than yours, when you don't know how good it is. ;) – Tom K. Jun 17 '16 at 08:06
  • Code added, its very rough and needs to be cleaned up (like for example not working with folder indexes which is a bit brittle, but the main problem is of course the loop through all the subfolders) – Sam Jun 17 '16 at 08:42

1 Answers1

1

Looping through Folder.Folders collections is the only way to find any given folder by name. The only alternative is to maintain an index of folder name and Folder.EntryID mappings so you can use NameSpace.GetFolderFromID.

Eric Legault
  • 5,706
  • 2
  • 22
  • 38