title: How to filter outlook mails on a subject using Python and win32com.client?
body: <h2><em>I was trying to download attachments from outlook which satisfy a particular subject but i got an exception that have no idea how to correct my code from below is my code:</em></h2>
  
  <pre><code>import win32com.client
  import os
  get_path = os.getcwd()
  outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI")
  inbox = outlook.GetDefaultFolder(6)
  messages = inbox.Items
  message2 = messages.GetLast()
  subject = message2.Subject
  body = message2.body
  sender = message2.Sender
  attachments = message2.Attachments
  for m in messages:
      if m.Subject == "Test Mail":
          for x in message2.Attachments:
              x.SaveASFile(os.path.join(get_path,x.FileName))
              print "successfully downloaded attachments"
  </code></pre>
  
  <h2>and here is the problem:</h2>
  
  <pre><code>Traceback (most recent call last):
    File "C:/Users/LENOVO USER/PycharmProjects/FlaskProject/EmailFilter/TestFile.py", line 4, in &lt;module&gt;
      outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI")
    File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
      dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
    File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
      return (_GetGoodDispatch(IDispatch, clsctx), userName)
    File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
      IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
  pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
  </code></pre>