如何使用pywin32获取outlook的默认文件夹以外的文件夹

2024-04-25 19:24:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用python的pywin32库制作一个警报邮件报告仪表板/报告(我是新来的), 因此,我试图获取警报邮件的时间等详细信息,该邮件已设置在默认收件箱文件夹以外的其他文件夹中(该文件夹也是其他文件夹的子文件夹,该父文件夹不属于任何默认outlook文件夹)

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()

因此,通过上面的代码,我只能获得默认文件夹中的电子邮件详细信息(如inbox/sent/)


Tags: ofthe文件夹getdatetimedateemail报告
1条回答
网友
1楼 · 发布于 2024-04-25 19:24:04

可以使用Stores属性,该属性返回一个Stores集合对象,该对象表示当前配置文件中的所有Store对象。Store类提供了GetDefaultFolder方法,该方法返回一个Folder对象,该对象表示存储中的默认文件夹,并且是由FolderType参数指定的类型。此方法类似于NameSpace对象的GetDefaultFolder方法。不同之处在于,此方法获取与帐户关联的传递存储上的默认文件夹,而NameSpace.GetDefaultFolder返回当前配置文件的默认存储上的默认文件夹。或者,您可以在存储区中的所有文件夹上进行迭代,如以下VBA宏示例代码所示:

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

相关问题 更多 >