Build an Array of PST files

I guess I should not be finding this difficult but I am getting Subscript out of range error with the following code and don't seem to be able to fix it.

I need to build an array of PST filestore names. Each PST file store begins with "~" with no spaces. I also want to add the Mailbox OST filestore as simply "Mailbox".

Thanks for the help
'Build Array of open filestores
    i = 0
    For Each oPST In oApp.Session.Folders
        If Left(oPST.Name, 1) = "~" Then
            'Add filestore to Array aMyStores
            aMyStores(i) = CStr(oPST.Name)
            i = i + 1
        End If
    Next
    aMyStores(i) = "Mailbox"

Open in new window

LVL 1
Charltp5Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David LeeCommented:
Hi, Charltp5.

In VBA arrays have to be set to a certain size when they are created.  How big did you make the array?  If you want the array to be dynamic, then you have to use the REDIM command to resize the array in the loop.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Charltp5Author Commented:
So I have amended the code to loop twice. Once to count the PST folders and one to create the array:

    'Build Array of open filestores
    i = 0
    For Each oPST In oApp.Session.Folders
        If Left(oPST.Name, 1) = "~" Then
            i = i + 1
        End If
    Next oPST
    ReDim aMyStores(i)
    i = 0
    For Each oPST In oApp.Session.Folders
        If Left(oPST.Name, 1) = "~" Then
            'Add filestore to Array aMyStores
            aMyStores(i) = CStr(oPST.Name)
            i = i + 1
        End If
    Next oPST
    aMyStores(i) = "Mailbox"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.