Search code examples
pythonoutlookwin32com

Running into error <unknown>.SaveAsFile, no solution yet found via any non-token sources for extracting files from enterprise outlook


I created the next part of the program to extract specific attatchment from my active outlook:

import win32com.client as win32
import win32com
from tabulate import tabulate
import os

def findFolder(folderName, searchIn):
    try:
        lowerAccount = searchIn.Folders
        for x in lowerAccount:
            if x.Name == folderName:
                objective = x
                return objective
        return None
    except Exception as error:
        print("Looks like we had an issue accessing the searchIn object")
        print(error)
    return None

outlook = win32com.client.Dispatch("Outlook.Application")
ons = outlook.GetNamespace("MAPI")
one = '[email protected]'

Folder1 = findFolder(one, ons)
inbox = findFolder('Postvak IN', Folder1)
messages=inbox.Items

ma = [["Subject", "Sender", "Attachment", "Saved?"]]
PathName = "C:\\Temp\\CV_BU_SQ"
os.chdir(PathName)
for msg in messages:
    if msg.Class == 43 and "TLS NL_Commvault" in msg.SenderName and len(msg.Attachments) == 1:
        CV_file = str(msg.Attachments.Item(1))
        CV_pf = os.path.join(os.getcwd() + '\\' + CV_file)
        res = "Yes!"
        try:
            msg.Attachments.SaveAsFile(os.getcwd() + '\\' + CV_file)
        except Exception as e:
            res = "No, error: " + str(e)
        ma.append([msg.Subject[:30], msg.SenderName[:30], CV_file, res])

print(tabulate(ma,headers="firstrow"))

The output is:

Subject                        Sender            Attachment                                            Saved?
-------------------------  ----------------  ----------------------------------------------------  -------------------------------
Backup Job Summary Report  TLS NL_Commvault  Commvault***DagelijkseBackup_2021-07-23-08-00-13.csv  No, error: <unknown>.SaveAsFile

or raw error:

Traceback (most recent call last):
  File "C:/Users/id983857/PycharmProjects/CheckCVMail/main.py", line 37, in <module>
    msg.Attachments.SaveAsFile(os.getcwd() + '\\' + CV_file)
  File "C:\Users\id983857\CheckCVMail\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.SaveAsFile

This outlook environment is based on Office 365, enterprise edition ... Any e-mail extraction software demo-edition can not be tested, as this is an enterprise edition. I do NOT need a token to read my mail per O365 or HTML, just my user account. I don't have access to an non-enterprise O365.

I want to know what could be the reason for this error?

Any value to the SaveAsFile(...) results in the same error.

I hope somebody has a idea how to fix this.


Solution

  • Change the line

    CV_file = str(msg.Attachments.Item(1))
    

    to

    CV_file = msg.Attachments.Item(1).FileName
    

    and

    msg.Attachments.SaveAsFile(os.getcwd() + '\\' + CV_file)
    

    to

    msg.Attachments.Item(1).SaveAsFile(os.getcwd() + '\\' + CV_file)