?
Solved

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

Posted on 2004-11-04
9
Medium Priority
?
1,233 Views
Last Modified: 2008-01-16
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?
...

0
Comment
Question by:Chi Is Current
  • 5
  • 4
9 Comments
 
LVL 26

Expert Comment

by:Alan Warren
ID: 12516910
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
 
LVL 2

Author Comment

by:Chi Is Current
ID: 12518220
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
 
LVL 2

Author Comment

by:Chi Is Current
ID: 12616573
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 26

Expert Comment

by:Alan Warren
ID: 12620561
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
 
LVL 2

Author Comment

by:Chi Is Current
ID: 12620729
Hi ALan ~

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

Thank You, Alan.
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 12621055
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
 
LVL 26

Accepted Solution

by:
Alan Warren earned 1000 total points
ID: 12621332
Ha ha !!!

Got it mate,.
Who needs those outlook experts ?

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


Alan
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 12621452
Hi Alan ~

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

Thank You, Jacob
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 12639286
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

840 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