Opening all .pst files in a file system folder

  • Thread starter Thread starter Stever
  • Start date Start date
S

Stever

I have a folder that contains many .pst files. I would like to click a
button on my form(in outlook) and have it open all of the .pst files that
are in c:\outlook\ . Is there a way to do this?

Thanks in advance

Steve
 
How do you walk through c:\outlook\ to retrieve the names of each of the
..pst files to put into the fullpath_to_the_pst_file variable?
 
It is really not Outlook specific and depends on the language you are using:
in C++/Delphi it is probably easier to use FindFirstFile/FindNextFile, or
use the Shell COM object.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Any way to do it using outlook vba? I am not a big programmer so I have to
use the simple tools ; )

Thanks
Steve
 
Then you need to use the Shell object:

set Shell = CreateObject("Shell.Application")
set Folder = Shell.Namespace("c:\outlook")
set Items = Folder.Items
for i = 0 to Items.Count-1
set Item = Items.Item(i)
if not Item.IsFolder Then
FileName =Item.Path
if UCase(Right(FileName, 4)) = ".PST" Then
Application.Session.AddStore(FileName)
End If
End If
next

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
That worked great! I thought it would be easy to just to the RemoveStore
but it doesn't look like that is the case. I get an error saying that the
removestore requires an object. I am sure this is a silly problem but I
can't seem to figure it out. Below is what I have so far. I have tried
several variations but I think I am in over my head. Any suggestions?

Thanks again

Set shell = CreateObject("Shell.Application")
Set Folder = shell.NameSpace("G:\Archive_Emails\zSteveTest")
Set Items = Folder.Items
For I = 0 To Items.Count - 1
Set Item = Items.Item(I)
If Not Item.IsFolder Then
FileName = Item.Path
If UCase(Right(FileName, 4)) = ".PST" Then
Length = Len(FileName)
trimmedlength = Length - 4
FinalName = Left(FileName, trimmedlength)
Application.Session.RemoveStore (FinalName)
End If
End If
Next
 
RemoveStore takes a folder object as a parameter, not a string. Pass the
root folder of that PST file retrieved from the Namespace.Folders
collection.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
That makes sense. The only thing I have left it trapping errors when all
the folders are already closed.
Here is what I ended up with for closing all the .pst files just in case
this thread gets ready by someone else.

Thanks for your help Dmitry

Private Sub btnCloseAll_Click()

Set objName = objOL.GetNamespace("MAPI")
Set shell = CreateObject("Shell.Application")
Set Folder = shell.NameSpace("G:\Archive_Emails\zSteveTest\")
Set items = Folder.items

For I = 0 To items.Count - 1
Set Item = items.Item(I)
ItemName = Item.Name

If Not Item.IsFolder Then
FileName = Item.Path

If UCase(Right(FileName, 4)) = ".PST" Then
lenItemName = Len(ItemName)
TrimmedLength = lenItemName - 4
FinalName = Left(ItemName, TrimmedLength)
Set objFolder = objName.Folders.Item(FinalName)
objName.RemoveStore objFolder
End If
End If
Next

End Sub
 
I don't think this will work - the name of the top folder in a PST file will
not generally be the same as the PST file name.
Unfortunately AddStore does not return a Folder object as one would expect.
To work around that, loop through all top level folders in the
Namespace.Folders collection and save their MAPIFolder.StoreID property in a
list or an array - this will be the list of your original stores.
Add the PST files using AddStore, do your processing. When the time comes to
unmount the PST files, loop through all folders in the Namespace.Folders
collection again and look at the MAPIFolder.StoreID property. If it is not
in the original list, that was a folder added using AddStore, pass that
folder to RemoveStore.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Back
Top