How to open and search a PST using VB or VBA

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

LVL 26
yo_beeDirector of Information TechnologyAsked:
Who is Participating?
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
 
tipsybroomCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Chris BottomleySoftware Quality Lead EngineerCommented:
Oops you can delete the find next ....it is not appropriate in the loop on restrict cases.


Chris
0
 
yo_beeDirector of Information TechnologyAuthor Commented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
What version of outlook?
0
 
yo_beeDirector of Information TechnologyAuthor Commented:
2003
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Any further help needed on this question?

Chris
0
 
yo_beeDirector of Information TechnologyAuthor Commented:
As of now no, but will post if needed.
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
 
yo_beeDirector of Information TechnologyAuthor Commented:
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
 
CodeCruiserCommented:
Have you looked at http:#37057882
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
 
yo_beeDirector of Information TechnologyAuthor Commented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
>>>  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
 
yo_beeDirector of Information TechnologyAuthor Commented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Lets agree to disagree, and i'll leave you to find a solution.

Chris
0
 
yo_beeDirector of Information TechnologyAuthor Commented:
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
 
yo_beeDirector of Information TechnologyAuthor Commented:
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
 
yo_beeDirector of Information TechnologyAuthor Commented:
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
 
yo_beeDirector of Information TechnologyAuthor Commented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
 
yo_beeDirector of Information TechnologyAuthor Commented:
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
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.

All Courses

From novice to tech pro — start learning today.