Link to home
Start Free TrialLog in
Avatar of Alan Warren
Alan WarrenFlag for Philippines

asked on

Reference a sub-sub-folder in secondary PST using VBA

Hello Outlook experts,

I use the following code in Ms Access 2000 to find a sub-sub-folder in an added PST catalog, it works, but I was wondering if there might be a more efficient way to do this.


Option Explicit

Dim olApp As Outlook.Application

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


I was hoping I could do something as simple as:
    Set olFolder = olNS.Folders("\\test\Inbox\lifeonline")  ' error: the operation failed an object could not be found

Had a bit of a look at the .Find method but I think it only applies to Items in current folder.



Alan
Avatar of Alan Warren
Alan Warren
Flag of Philippines image

ASKER

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

Alan  ":0)
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ralf Klatt
Hi,

@alanwarren ... however ... your little peace of code was a great help to me in some kind of a same situation ... the level where Outlook Folder / Item & Items access can get down to has really astonished me ... but there are huge ways of usage ... thanks alanwarren!


Best regards,
Raisor