0

I try to make an alert mail report dashboard/report using python's pywin32 library(I'm new into this), so I'm trying to fetch the details like time of an alert mail that has been set up in some different folder other than the default inbox folder(that is also a subfolder of a different folder altogether, which parent folder is not part of any default outlook folder).

My outlook folder

import sys, win32com.client, datetime
# Connect with MS Outlook - must be open.
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

# connect to Sent Items
s = outlook.GetDefaultFolder(6).Items   # "5" refers to the sent item of a
#s.Sort("s", true)
# Get yesterdays date for the purpose of getting emails from this date
d = (datetime.date.today() - datetime.timedelta (days=1)).strftime("%d-%m-%y, %H:%M:%S")
# get the email/s
msg = s.GetLast()
# Loop through emails
while msg:
    # Get email date 
    date = msg.SentOn.strftime("%d-%m-%y , %H:%M:%S")
    # Get Subject Line of email
    sjl = msg.Subject
    # Set the critera for whats wanted                       
    if d == date and (msg.Subject.startswith("XXXX ") or msg.Subject.startswith("XXXXX")):
        print(sjl,date) 
    msg = s.GetPrevious()

So by the above code I was only able to get the details of email which are in the default folder only (such as inbox/sent/..)

1 Answers1

0

You can use the Stores property which returns a Stores collection object that represents all the Store objects in the current profile. The Store class provides the GetDefaultFolder method which returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile. or you can just iterate over all folders in the store like shown in the following VBA macro sample code:

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
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45