PDA

View Full Version : Add Data File Prorgammatically



Mavyak
01-14-2015, 01:00 PM
I have a program that generates a *.pst file from email data stored in a database.

When I manually attach the *.pst file as follows, the top-level folder shows as "Personal folders" and has all the sub-folders and their contained items like they should:
Click File-->Account Settings-->Accoutn Settings...
Click "Data Files" tab
Click "Add..."
Choose "outlook data file (.pst)" and click "OK"
Navigate to and choose *.pst file in "Create or Open Outlook Data File" dialog and click "OK"

If I use VBA to do this, the top-level folder sometimes displays as "Outlook Data File" and only contains a "Deleted Items" and "Search Folders" folder.
However, sometimes it displays as "Personal folders" and contains the Inbox (and all other sub-folders).

I'm talking about the same *.pst file in both instances.

Here is the VBA I am using to add the data files:

Public Sub AddStores(folderPath As String)
Dim MyObj As Object, MySource As Object
Dim file As Variant

Dim olkNS As Outlook.NameSpace
Set olkNS = Application.GetNamespace("MAPI")

file = Dir(folderPath)

While (file <> "")

If ".pst" = LCase(Right$(file, 4)) Then olkNS.AddStoreEx file, olStoreDefault


file = Dir

Wend

Set olkNS = Nothing

End Sub

How can I use VBA to attach the data file so that the root folder is "Personal folders" and all the sub-folders display and are available?

Mavyak
01-14-2015, 01:13 PM
Update:
If I add a *.pst file using the GUI and, while said *.pst is still attached (with top-level folder as "Personal folders"), run the VBA to attach the data files, the data files get attached properly.

Mavyak
01-15-2015, 10:21 AM
We generate hundreds of *.pst files at a time. The VBA code I wrote attaches the *.pst files and then iterates over their contents to generate an analysis report for the person that generated the *.pst files to use for process validation. So this code needs to be able to dynamically add and remove stores at will.

Any thoughts on how I can make sure the *.pst file gets added properly such that they always display as "Personal folders" with the correct folder structure and item content vs displaying as "Outlook Data File" without the proper folder structure and item content?

Any help here would be greatly appreciated.

Mavyak
01-26-2015, 11:39 AM
BUMP