Cycle through all folders under Mailbox

J

Jeremy

Hi,

I would like to cycle through every folder in my mailbox and I have the
following code. However, i don't think this code will cycle through all
folders, I think it just cycles through those under my inbox. I don't think
the macro will cycle through folders located under .pst archives, or other
mailboxes that I have access to under my mailbox.

Is there a way a cycling through all folders, including those under other
mailboxes and archives?

My current macro is as follows:

Sub ProcessAllFolders()
ProcessSubFolder Application.ActiveExplorer.CurrentFolder
MsgBox "All Done!", vbInformation + vbOKOnly, "Process All Folders Macro"
End Sub

Sub ProcessSubFolder(olkFolder As Outlook.MAPIFolder)
Dim olkSubFolder As Outlook.MAPIFolder

'insert some code here to do something

For Each olkSubFolder In olkFolder.Folders
ProcessSubFolder olkSubFolder
Next
Set olkSubFolder = Nothing
End Sub
 
A

Alan Moseley

You just need to start your search higher up the folder tree eg.

Public Sub ProcessAllFolders()
Dim fld As MAPIFolder
For Each fld In Application.GetNamespace("MAPI").Folders
Call ProcessFolder(fld)
Next fld
End Sub
Public Sub ProcessFolder(ByRef fld As MAPIFolder)
Dim subfld As MAPIFolder
'Do Something here
For Each subfld In fld.Folders
Call ProcessFolder(subfld)
Next subfld
End Sub
 
J

Jeremy

Alan,

Thanks for the code. It's very helpful.

I wantedto loop through all my folders and apply a specified view. Adapting
your code I have the following:

Public Sub ProcessAllFolders()
Dim fld As MAPIFolder
For Each fld In Application.GetNamespace("MAPI").Folders
Call ProcessFolder(fld)
Next fld
End Sub
Public Sub ProcessFolder(ByRef fld As MAPIFolder)
Dim subfld As MAPIFolder

'do something here
Application.ActiveExplorer.CurrentView = "J VIEW"

For Each subfld In fld.Folders
Call ProcessFolder(subfld)
Next subfld
End Sub


I understand I need to get the current view as an object and apply it but
I'm not sure of the code. If you're inclined to any further help I do
appreciate it!


Thanks
 
J

Jeremy

Hi,

I seem to have some odd results with this code. It iterates through an
apparently random selection of folders, and then fails to apply the specified
view. Is there an easy solution? I *really* wanted to be able to apply a
specified view to all folders but it seems impossible and I'm wasting hours
trying to work this out.

The code I have is:


Public Sub ProcessAllFolders()
Dim fld As MAPIFolder

For Each fld In Application.GetNamespace("MAPI").Folders
Call ProcessFolder(fld)
Next fld

End Sub
Public Sub ProcessFolder(ByRef fld As MAPIFolder)
Dim subfld As MAPIFolder

For Each subfld In fld.Folders
Call ProcessFolder(subfld)

If subfld.DefaultItemType = olMailItem Then

ActiveExplorer.CurrentFolder.Views("J VIEW").Apply
ActiveExplorer.CurrentFolder.Views("J VIEW").Save

End If

Next subfld

End Sub



Thanks for any help :)
 
A

Alan Moseley

The iteration through the folders is not at all random. For example, it
begins with your first mailbox, selecting each folder in turn until it
reaches the end. It then will select (probably) your archive and repeat the
process, and then your public folders.

Secondly, ActiveExplorer is your folder list as displayed in Outlook, not
the list of folders that you are iterating through. Therefore, as your code
was iterating through each folder, it was just reapplying your custom view to
whichever folder you had visible in Outlook. You will need to change these
lines:-

ActiveExplorer.CurrentFolder.Views("J VIEW").Apply
ActiveExplorer.CurrentFolder.Views("J VIEW").Save

with:-

subfld.Views("J VIEW").Apply
subfld.CurrentFolder.Views("J VIEW").Save
 
J

Jeremy

Alan,

I think I'm almost there. What i find is that the following code (below)
works fine if I choose the "Messages" view, but stops on the line

subfld.Views("J View").Apply

when I try using a custom view. It says "Object variable or With block
variable not set". I can't fathom which object variable I have not set. Any
clue?



Public Sub ProcessAllFolders()
Dim fld As MAPIFolder

For Each fld In Application.GetNamespace("MAPI").Folders
If fld.DefaultItemType = olMailItem Then
Call ProcessFolder(fld)
End If
Next fld

End Sub
Public Sub ProcessFolder(ByRef fld As MAPIFolder)
Dim subfld As MAPIFolder

For Each subfld In fld.Folders

Call ProcessFolder(subfld)
If subfld.DefaultItemType = olMailItem Then
subfld.Views("J View").Apply
subfld.Views("J View").Save
End If

Next subfld

End Sub
 
A

Alan Moseley

Find out which folder the code is failing at. Now browse to that folder in
your folder list. You will probably find that 'J VIEW' is not a view that
you can choose from. 'J VIEW' must exist for you to be able to select it.

I guess that your next question is going to be 'how do you copy a view from
one folder to another? If so, please start a new thread.
 
J

Jeremy

Hi Alan,

I did think of that. "J View" is available to "All Mail and Post Folders".

The macro seems to crash on the first folder it goes to, which in my case is
the 'deleted item' sub folder in a pst. I hovered the cursor over "subfld"
and "fld" to see what floders were referenced when the macro failed. It fails
on the first cycle, and "J View" is available in deleted items; so i'm at a
bit of a loss.

Thanks for keeping coming back to me. If you got further thoughts/soultions
it's a help. I sit here for ages staring at the code thinking "maybe....". I
appreciate you may want to forget about this issue now though.


Best regards

Jeremy
 
J

Jeremy

Alan,

I have a solution.

I set up another view and tried applying that to all folders and it worked.
I wonder if "J View" just acquired some bug at somepoint. Frustrating!

In any case, the macro does seem to work perfectly, cycling through all
folders and applying the view.

I'm really pleased. Thank you for all your help. You've solved many problems
that I've had in this one thread.

Best regards


Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top