?
Solved

How to open and search a PST using VB or VBA

Posted on 2011-10-31
29
Medium Priority
?
582 Views
Last Modified: 2012-05-12
Question I have is how can I connect to a PST and Search the To and From fields for a certain group of addresses.
I figure that it would be something similar to the code below

 
Sub MoveItems()
 
         Dim myOlApp As New Outlook.Application
 
         Dim myNameSpace As Outlook.NameSpace
 
         Dim myInbox As Outlook.MAPIFolder
 
         Dim myDestFolder As Outlook.MAPIFolder
 
         Dim myItems As Outlook.Items
 
         Dim myItem As Object
 
         Set myNameSpace = myOlApp.GetNamespace("MAPI")
 
         Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
 
         Set myItems = myInbox.Items
 
         Set myDestFolder = myInbox.Folders("Personal Mail")
 
         Set myItem = myItems.Find("[SenderName] = 'Dan Wilson'")
 
         While TypeName(myItem) <> "Nothing"
 
                 myItem.Move myDestFolder
 
                 Set myItem = myItems.FindNext
 
         Wend
 
End Sub

Open in new window

0
Comment
Question by:yo_bee
  • 13
  • 11
  • 2
  • +1
27 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37056962
I generally use filters i.e.

Chris
Sub MoveItems()

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.items
Dim myItem As Object
Dim strFilter As String

    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
    Set myItems = myInbox.items
    Set myDestFolder = myInbox.Folders("Personal Mail")
    strFilter = "[SenderName] =" & "Dan Wilson"
    Set myItems = myInbox.items.Restrict(strFilter)
    For Each myItem In myItems
        myItem.Move myDestFolder
        Set myItem = myItems.FindNext
    Next

End Sub

Open in new window

0
 
LVL 7

Expert Comment

by:tipsybroom
ID: 37057004
you can search items in a specific folder:

Set myItem = myItems.Find("[SenderAddress] = 'Dan.Wilsonyahoo.com'")

Open in new window


[SenderAddress] = mail-address of sender
[RecieverAddress] = mail-address of receiver

but I'm not sure

further read this: http://msdn.microsoft.com/en-us/library/bb220350%28v=office.12%29.aspx
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37057067
Oops you can delete the find next ....it is not appropriate in the loop on restrict cases.


Chris
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 37057380
Back onto a proper 'puter so ...

Chris
Sub MoveItems()

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.items
Dim myItem As Object
Dim strFilter As String

    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
    Set myItems = myInbox.items
    Set myDestFolder = myInbox.Folders("Personal Mail")
    strFilter = "[SenderName] =" & "Dan Wilson"
    Set myItems = myInbox.items.Restrict(strFilter)
    For Each myItem In myItems
        myItem.Move myDestFolder
    Next

End Sub

Open in new window

0
 
LVL 24

Author Comment

by:yo_bee
ID: 37057700
Question I have is Line 12 and 14.
the GetDefaultFolder is going to be the PST Root folder?
That is what I am curious about.

I have 56 PST to loop through and I want to automate it a bit.

Currently I mount each PST in a Outlook Profile and run two rules against each 1 of the 56 PSTs. and if found move a copy to another PST. :
1: From a Jack to either John or Frank
2: From John or Frank to the Jack

So what I am looking for is to AddStore then search the PST for all mail items once finished closestore and move onto the next PST.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37058259
What version of outlook?
0
 
LVL 24

Author Comment

by:yo_bee
ID: 37058362
2003
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37059004
Addstore and removestore methods will do that for you as you rightly surmised and once you have added each in turn you need to cycle through the emails in the relevant pst as above.

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37061391
Any further help needed on this question?

Chris
0
 
LVL 24

Author Comment

by:yo_bee
ID: 37062650
As of now no, but will post if needed.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37063175
Since it sounds and seems that the requirement is met I suggest paq is more appropriate and a follow on question, (related) question asked if needed later.

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37068072
yo_bee

As a Qualified Expert, I would expect you to be aware that it is considered polite to keep questions in a thread to the immediate topic of the question.  i.e. the original request was in regard to isolating mail sets using for example the senderaddress.  I am suggesting you have this solution already in the thread and that therefore the polite action is to close off the question appropriately.

For what it's worth I was intrigued by your follow on question regarding opening and closing multiple psts during the activity and noting the limited support for handling psts in 2003 I have gone ahead and confirmed to myself that what I said was possible ... is possible and therefore if you choose to take the question forward with a related question then an improved solution handling multiple psts can be supported by any appropriate expert.

Chris
0
 
LVL 24

Author Comment

by:yo_bee
ID: 37140812
So this line here will connect to a PST
Set myDestFolder = myInbox.Folders("Personal Mail")

Open in new window


But is the previous lines state :
 
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)

Open in new window


Wouldn't that be connecting to the Mailbox Name inbox?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37140835
Have you looked at http:#37057882
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37140855
Both statements connect to a folder in the default PST:

myInbox is ... the inbox of the default PST and mydestfolder is a subfolder of the inbox

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37140884
Note again as I mentioned earier the initial question was in respect of the default PST.  If you want to open multiple PST's then this is a different matter but can be done.  Note however the desire to open multiple PSTs is a deviation from the initial question and not a responsible activity to pursue in this thread ... in my opinion.

Chris
0
 
LVL 24

Author Comment

by:yo_bee
ID: 37141214
Yes my question was "How to open and search a PST using VB or VBA"

So How to Open  Namespace.addstore then Search that PST.

So I am not sure how I am veering off the question here.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37141318
>>>  How to Open  Namespace.addstore then Search that PST.

The original question question included searching / filtering / find on the to / from of a specific PST.  I believe you have that answer already from 2 weeks ago.  The extended aspect to the question was to cycle over 56 PST's and hence that aspect is in my view not per the original question ... indeed your comment in 37062650 indicated there was no further help required at the time implying the original requirement was met.

Chris
0
 
LVL 24

Author Comment

by:yo_bee
ID: 37142575
Please reread  my question:


Title:
How to open and search a PST using VB or VBAQuestion:
Question I have is how can I connect to a PST and Search the To and From fields for a certain group of addresses.
I figure that it would be something similar to the code below


I think this is getting lost in translation.

So let me rephase this question:

I am looking to attach to a PST that is not already opened by a mapi client.  Once Opened I would like to search the entire PST for any Mail items that contain a certain TO and From criteria.

So I am pretty sure that is basicly the same thing, but if not that is what the goal is.  I just used the snippet as an example.

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37143705
Lets agree to disagree, and i'll leave you to find a solution.

Chris
0
 
LVL 24

Author Comment

by:yo_bee
ID: 37145586
So how does this line connect to the PST:

 
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)

Open in new window


Since you have mentioned that it worked.  
If the PST Name is "Test"
Would I replace the olFolderInbox with Test?

0
 
LVL 24

Author Closing Comment

by:yo_bee
ID: 37164216
I found Chris to be pushy and not very helpful or understanding to my original question. I wish I could have awarded him -2000 points for his rudeness. So I will do the next thing I can do and award a C with all negative remarks.
0
 
LVL 24

Author Comment

by:yo_bee
ID: 37164221
Midus
I will accept Chris's solution, but I have not been able to apply his suggestion.

With regards to your reply about waiting 2 weeks to reply. That is incorrect. Chris started pushing to close this two days after I posted the original question and before I started to ask additional questions. That is why I reached out for additonal assistance.  

I will accept and repost.
0
 
LVL 24

Author Comment

by:yo_bee
ID: 37164586
This was not a punishment, but a grade on the quality which was extremely poor in my eyes.

I do not feel you nor anyone at this site has a right to change a grade.
If I could delete this question I would, but I know how this works so I need to give an accepted answer and a grade. Since I felt it was C quality work it should remain a C.

The answer was accepted and should be closed without any further alteration to the grading.

I will post a clear stated question later and hope that it is not misunderstood.

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37165476
Yo-Bee

I had turned off monitoring of this thread as stated earlier hence the slow response but, I can see where my understanding was in error, (thank you modus_in_rebus for the direction here).  Out interest though if I had been aware that it was two questions, (cycle through every PST and filter on items) at the outset I would have ignored the thread in it's entirety.

Please appreciate my post stating lets agree to disagree and i'll leave you to find a solution was recognising we had a disagreement and I 'left' the thread to enable you get a further answer to the matter of the PST cycles.  The disagreement aspect was simply that ... a disagreement.

Anyway this is neither the time nor the place for a rant so down to the purpose for 'breaking' my intention to walk away.

Although as stated I investigated a solution to the multiple PST part of the question I will as a courtesy to your apparent dislike of my involvement here avoid polluting your new question with any posts and wish you well in your involvement on the site.  

I have however re-enabled monitoring of this specific question so that if you find you would like my help on that question in the future I will hopefully pick up on such a request here ... if I miss any such request I apologise but it will be oversight NOT rejection as the sole purpose of this post is to let you know I will try to help if required but keep away if not.

Chris
0
 
LVL 24

Author Comment

by:yo_bee
ID: 37165481
You comments are well taken and much appreciated. I just was looking for some assistance and did not mean to mislead or not follow protocol.

Mike
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 trialware OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their Exchange server is no longer available or other critical issues with Exchange server or impo…
This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question