Modify VBA code to search Outlook PST folder, other than default

Hello ~

The following piece of code searches for email messages in the "lifeonline" folder, under the Inbox, under the default PST folder - beautifully.

I would like to modify the code to search for email messasges in the "lifeonline" folder, under the Inbox, under an alternate PST folder, named: TEST.

Can you suggest a modification of this code to describe this new path?

I envision the modification to:
Set olFolder = olNS.GetDefaultFolder(olFolderInbox)

Many Thanks, Jacob

P.S. - I'll be offline for a while; will check for replies in a ferw hours.

-------------------------------------------------------------------------------------

Option Compare Database
Option Explicit

Dim olApp As Outlook.Application
Const sLifeDataFolder = "lifeonline"

Public Sub GetMail()
 
' Reference: Microsoft Outlook Object Library
'
' Authors:     Jacob Barnett, Alan Warren
' Modified:   03-Jun-2004
'
' Usage: Call GetMail
 
 
  Dim olNS As NameSpace
  Dim olRec As Recipient
  Dim olFolder As MAPIFolder
  Dim olDataFolder As MAPIFolder
 
  Dim Item                          ' As MailItem
 
  Set olApp = New Outlook.Application
  Set olNS = olApp.GetNamespace("MAPI")  ' open the MAPI Namespace
  Set olFolder = olNS.GetDefaultFolder(olFolderInbox) ' open the Inbox  for the user whoes account we are using
 
  Set olDataFolder = olFolder.Folders(sLifeDataFolder)

  If olDataFolder.Items.Count = 0 Then: Exit Sub
  DoCmd.Hourglass True
  For Each Item In olDataFolder.Items   ' for each mail item in the folder
    ' is the item as yet un-read?
...

LVL 2
Chi Is CurrentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alan WarrenApplications DeveloperCommented:
Hi Jacob,

hows things with you?

Had a bit of a look into the msdn library on namespace, think you can use the NameSpace object's Logon method to specify a profile programmatically. Now I'm not sure if you are actually running multiple profiles in your outlook instance  or if you intend to only do this programmatically. I will research it some more, probably I need to set up multiple profiles in order to test this.

Dim objApp As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
       
Set objApp = New Outlook.Application
Set objNameSpace = objApp.GetNamespace(Type:="MAPI")

' objNameSpace.Logon [Profile], [Password], [ShowDialog], [NewSession]
objNameSpace.Logon "profile", "password", False, True


Take care...


Alan

Understanding the Application and NameSpace Objects
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/html/deovrunderstandingapplicationnamespaceobjects.asp

0
Chi Is CurrentAuthor Commented:
Thank you, Alan -

'Will check out your recommendation later today.  I will also find out about multiple profiles.  I do not think it is an issue.

Best Regards, Jacob
0
Chi Is CurrentAuthor Commented:
Hi Alan ~

I'm good.  Busy and not busy at the same time.

Your idea here does not address the situation here.  I think I may not have explained it clearly.  The code doesn't need to logon to a new profile, as Outlook is already open and running within the user's profile.  It just needs look for messages in a secondary (additional) PST folder that is ALSO opened in Outlook, which happens to exist on an exchange server.

In otherwords, my "root" PST folder is: Outlook Today - [Jacob's], which contains Inbox, Sent Items, etc.  A second PST directory exists on the same level as the "root" and is named: TEST, containing an Inbox, Deleted items, etc.  The messages are now in this secondary Inbox folder, in a sub-folder named "lifeonline", under the TEST PST folder (TEST\Inbox\lifeonline: messages).  Originally the messages could be found in: (Outlook Today - [Jacob's]\Inbox\sLifeData).

'Seems to be a matter of setting olFolder to TEST\Inbox instead of the default\Inbox ...

Set olFolder = olNS.GetDefaultFolder(olFolderInbox) ' open the Inbox  for the user whoes account we are using
 
Is there an easy way to do this?

Jacob
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.

Alan WarrenApplications DeveloperCommented:
Hey Jacob ":0)

I have set up the second pst and now have added the datafile to my Outlook, created the Inbox subfolder lifeonline, looking good so far.

I dont have exchange here but I figure it will be ok

Will let you know when I work it out.

Alan


0
Chi Is CurrentAuthor Commented:
Hi ALan ~

Yes, I don't think Exchange is necessary, just the second PST folder(?).

Thank You, Alan.
0
Alan WarrenApplications DeveloperCommented:
Hi Jacob,

got this working but it a bit lumpy, like to find a better way to do it.

Option Explicit

Dim olApp As Outlook.Application
Const sLifeDataFolder = "lifeonline"

Public Sub GetMail()
 
' Reference: Microsoft Outlook Object Library
 
  Dim olNS As NameSpace
  Dim olFolder As MAPIFolder
 
  Set olApp = New Outlook.Application
  Set olNS = olApp.GetNamespace("MAPI")  ' open the MAPI Namespace
 
  Dim fol As MAPIFolder
  Dim subFolder As MAPIFolder
  Dim subsubfolder As MAPIFolder
 
  Dim strEntryID As String
 
  For Each fol In olNS.Folders
    Debug.Print fol.Name, fol.FolderPath
    For Each subFolder In fol.Folders
      Debug.Print "  -" & subFolder.Name, subFolder.FolderPath, subFolder.EntryID
      If subFolder.FolderPath = "\\test\Inbox\lifeonline" Then: strEntryID = subFolder.EntryID
      For Each subsubfolder In subFolder.Folders
        If subsubfolder.FolderPath = "\\test\Inbox\lifeonline" Then: strEntryID = subsubfolder.EntryID
        Debug.Print "    -" & subsubfolder.Name, subsubfolder.FolderPath, subsubfolder.EntryID
      Next
    Next
  Next

  If strEntryID = "" Then
    MsgBox "lifeonline not found"
    Exit Sub
  Else
    Set olFolder = olNS.GetFolderFromID(strEntryID)
  End If
 
 

End Sub


Have posted a Q to the Outlook topic area, see if the experts there can come up with a one-liner for us.
  Find a sub-folder in secondary PST using VBA
  http://www.experts-exchange.com/Applications/MS_Office/Outlook/Q_21212655.html


Alan

0
Alan WarrenApplications DeveloperCommented:
Ha ha !!!

Got it mate,.
Who needs those outlook experts ?

  Set olFolder = olNS.Folders.Item("test").Folders.Item("Inbox").Folders.Item("lifeonline")


Alan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chi Is CurrentAuthor Commented:
Hi Alan ~

'Looks like the winner.  I'll check it out.

Thank You, Jacob
0
Chi Is CurrentAuthor Commented:
Hi Alan ~

Set olFolder = olNS.Folders.Item("test").Folders.Item("Inbox").Folders.Item("lifeonline")

Is it!!  Works beautifully.  Thank you for the path syntax in Outlook.  ANd thank you for the links on NameSpace objects and Folders & Items, they are helpful also.

Best Regards, Jacob
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

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.