Accessing .pst folder in outlook using VBA Code

A

Abhijat

Hi group,
I need some help regarding accessing .pst folder using VBA. I want to
replicate the code code below (done for Inbox) for a folder in my .pst
(the path of .pst file is say "C:\Outlook\Personal Folders.pst"

Set ns = appOl.GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
For Each Item In Inbox.Items
---
---
Next Item


Any help in this regard with be truly appreciated.

Best Regards,
Abhijat
 
M

Michael Bauer [MVP - Outlook]

If the file isn't opend yet, see the AddStore method. Then the store is
available in the Namespace.Folders collection.

--
Best regards
Michael Bauer - MVP Outlook

: Outlook Categories? Category Manager Is Your Tool
: VBOffice Reporter for Data Analysis & Reporting
: <http://www.vboffice.net/product.html?pub=6&lang=en>


Am Mon, 12 Oct 2009 09:29:22 -0700 (PDT) schrieb Abhijat:
 
A

Abhijat

Hi Michael,
Thanks a lot.
Yes. I can open the .pst file as
ns.AddStore ("P:\Outlook\Personal Folders.pst"). So the code now
becomes:
Set ns = appOl.GetNamespace("MAPI")
ns.AddStore ("P:\Outlook\Personal Folders.pst")

Its fine till here.

My problem is : my code is not able to point at a sub-folder (call it
"TEST") which is made in the .pst file.

Just to reiterate:
I can have a code that can point at the "Test" sub-folder under my
server Inbox like this:
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set newInbox = Inbox.Folders("Test")

I am looking for a code that may point at the "TEST" sub-folder lying
under my .pst file.

Thanks for your help again!

Best regards,
Abhijat.
 
M

Michael Bauer [MVP - Outlook]

The store has a Folders collection, that's where you start to find your
folders by name.

Unfortunately, AddStore doesn't return the added folder. So, you need to
find it in Session.Folders, maybe by comparing what has been there before
and what's new. You cannot rely on the index.

It's a lot easier with the Redemption (www.dimastr.com). Its AddPSTStore
function does return the added store.

--
Best regards
Michael Bauer - MVP Outlook

: Outlook Categories? Category Manager Is Your Tool
: VBOffice Reporter for Data Analysis & Reporting
: <http://www.vboffice.net/product.html?pub=6&lang=en>


Am Tue, 13 Oct 2009 03:46:52 -0700 (PDT) schrieb Abhijat:
 
A

Abhijat

Hi Michael,
Thanks a lot.
I think I need to work on my skills before I can use your suggestion.
Once again, thanks a lot for your time and guidanace.

Best regards,
Abhijat.
 
Joined
Mar 23, 2017
Messages
1
Reaction score
0
Hello Everyone. I know it is a little bit late, but just in case there is someone who is still asking how to access to a pst list of folders or mails through Outlook vba, i found a way to do it. You can use the following code:

Sub TestSub()
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
'Add PST file
Ns.AddStore ("C:\PathToPST\PSTArchive.pst")
Set ItemsPST = Ns.Session.Folders.Item(2).Items
End Sub

Item(2) is the PST itself, so it contains some PST expected properties such as "Items", which is the list of emails the pst contains, "Folders", which is the array that contains all the folders the pst has, and do on.

Note: You can take a look at all the properties the pst has by adding a watcher to "Item(2)". To do that, you can declare a variable called "pstObject" in the following way, and then right-click the variable and click the option "Add Watch..." :

Set pstObject : Ns.Session.Folders.Item(2)

This is a screenshot of how the watcher should look:

Object.png


I hope this helps.
 

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