none
Access to different pst to file emails RRS feed

  • Question

  • Hi Guys,

    i would like to have my additional PST file selected via vba to store my emails - this is on the same level as my main post martin.xxx@yy.de

    Got code

    Sub Filing()
    
    On Error Resume Next
    
    Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
    Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem
    
    Set objNS = Application.GetNamespace(“MAPI“)
    Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
    
    Set objFolder = objInbox.Folders("Filing")
    
    If objFolder Is Nothing Then
    MsgBox "This folder doesnt exist"
    End If

    Below the inbox is a folder Filing- here it says, folder doesn't exist - but I actually want to access a PST folder folder (without subfolders) and file the emails there - someone an idea?


    structure

    martin.x@y.de-> Inbox-> filing


    Filing (own PST)


    Thank you!

    Martin

    Thursday, June 25, 2020 12:13 PM

All replies

  • It seems you are dealing with an additional store in the profile. So, you need to find the required store first. You can do that by using the Stores property of the Namespace class. For example, here is how to iterate over all stores and folders:

    Sub EnumerateFoldersInStores() 
     
     Dim colStores As Outlook.Stores 
     
     Dim oStore As Outlook.Store 
     
     Dim oRoot As Outlook.Folder 
     
     
     
     On Error Resume Next 
     
     Set colStores = Application.Session.Stores 
     
     For Each oStore In colStores 
     
     Set oRoot = oStore.GetRootFolder 
     
     Debug.Print (oRoot.FolderPath) 
     
     EnumerateFolders oRoot 
     
     Next 
     
    End Sub 
     
     
     
    Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder) 
     
     Dim folders As Outlook.folders 
     
     Dim Folder As Outlook.Folder 
     
     Dim foldercount As Integer 
     
     
     
     On Error Resume Next 
     
     Set folders = oFolder.folders 
     
     foldercount = folders.Count 
     
     'Check if there are any folders below oFolder 
     
     If foldercount Then 
     
     For Each Folder In folders 
     
     Debug.Print (Folder.FolderPath) 
     
     EnumerateFolders Folder 
     
     Next 
     
     End If 
     
    End Sub

    So, after you have found the required store you can use the Store.GetDefaultFolder method and get a subfolder.



    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, June 25, 2020 3:40 PM