• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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

0
Charltp5
Asked:
Charltp5
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now