【问题标题】:How get folder other than default folder of outlook using pywin32如何使用pywin32获取outlook默认文件夹以外的文件夹
【发布时间】:2023-04-15 21:18:01
【问题描述】:

我尝试使用 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()

所以通过上面的代码,我只能获取默认文件夹中的电子邮件详细信息(例如收件箱/已发送/..)

【问题讨论】:

    标签: python python-3.x outlook pywin32 win32com


    【解决方案1】:

    您可以使用Stores 属性返回一个代表当前配置文件中所有Store 对象的Stores 集合对象。 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
    

    【讨论】:

    • 谢谢? 我会试试的