Here I am trying to extract one particular email data which is present in sent items folder in outlook? but I am unable to find any method to give such condition , like if this particular email present in sent item folder then extract subject, date & time.
import win32com.client
from win32com.client import Dispatch
import regex as re
from datetime import datetime, timedelta
outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
sentitem = mapi.GetDefaultFolder(5)
messages = sentitem.Items
for msg in messages:
here after I am not getting any idea. Can anyone help me to find this
CodePudding user response:
You cannot use regex with the Outlook Object Model, but you can use Items.Find/FindNext
and Items.Restrict
to find the message(s).
See https://docs.microsoft.com/en-us/office/vba/api/outlook.items.restrict for the syntax and sample queries.
CodePudding user response:
Iterating over all items and checking item's properties is not really a good idea when dealing with the Outlook object model:
for msg in messages
Instead, the OOM provides three main ways for getting the job done:
Use the
Find
/FindNext
methods of theItems
class. See How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET) for more information.Use the
Restrict
method of theItems
class. This method is an alternative to using theFind
method orFindNext
method to iterate over specific items within a collection. TheFind
orFindNext
methods are faster than filtering if there are a small number of items. TheRestrict
method is significantly faster if there is a large number of items in the collection, especially if only a few items in a large collection are expected to be found. See How To: Use Restrict method to retrieve Outlook mail items from a folder for more information.The
AdvancedSearch
method of theApplication
class. The key benefits of using the AdvancedSearch method in Outlook are:- The search is performed in another thread. You don’t need to run another thread manually since the
AdvancedSearch
method runs it automatically in the background. - Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The
Restrict
andFind
/FindNext
methods can be applied to a particularItems
collection (see theItems
property of theFolder
class in Outlook). - Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the
IsInstantSearchEnabled
property of theStore
class). - You can stop the search process at any moment using the
Stop
method of theSearch
class.
Read more about the
AdvancedSearch
method in the Advanced search in Outlook programmatically: C#, VB.NET article.- The search is performed in another thread. You don’t need to run another thread manually since the