2014-05-16 3 views
2

Outlook에서 첨부 파일을 가져 오는 다음 스크립트가 있습니다.Python을 사용하여 Outlook 2010에서 첨부 파일 받기

outlook = win32com.client.Dispatch("Outlook.Application") 
inbox = outlook.GetDefaultFolder(0) 
messages = inbox.Items 
message = messages.GetLast() #open last message 
attachments = message.Attachments #assign attachments to attachment variable 
attachment = attachments.Item(1) 
attachment.SaveASFile(os.path.join('c:', 'temp')) 

내가 그러나 나는 다음과 같은 얻고 그것을 실행하는 경우 :

Traceback (most recent call last): 
File "C:/Users/e003048/QA/trunk/automation/selenium/src/global_functions/util_get_email_attachments.py", line 11, in <module> 
inbox = outlook.GetDefaultFolder(0) 
File "C:\Python27\Lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__ 
raise AttributeError("%s.%s" % (self._username_, attr)) 
AttributeError: Outlook.Application.GetDefaultFolder 

나는이 작업을 얻을 수있는 사용자 이름을 둘 것입니다 어디 확실입니다.

나는 아래의 대답 제안을 시도하고 다음과 같은 오류가 무엇입니까 :

Traceback (most recent call last): 
File "C:/Users/e003048/QA/trunk/automation/selenium/src/global_functions/util_get_email_attachments.py", line 10, in <module> 
inbox = mapi.GetDefaultFolder(0) 
File "<COMObject <unknown>>", line 2, in GetDefaultFolder 
pywintypes.com_error: (-2147024809, 'The parameter is incorrect.', None, None) 

내 완료 작업 코드를 포함하고 싶었 다른 사람이 유용하게 찾을 수에서의 경우 :

def get_email_attachments(self): 
    outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace('MAPI') 
    # change the Folders parameter to the directory you are having the attachment go to 
    inbox = outlook.GetDefaultFolder(6).Folders('ForAttachments') 
    messages = inbox.Items 
    message = messages.GetLast() # opens the last message 
    attachments = message.Attachments 
    attachment = attachments.Item(1) 
    attachment.SaveAsFile('C:\\temp\\' + attachment.FileName) 

답변

4

GetDefaultFolder은 응용 프로그램 개체 (application docs 참조)에 정의되어 있지 않으며

을 통해 가져 오는 NameSpace 개체에 정의되어 있습니다
mapi = outlook.GetNameSpace("MAPI") 

그런 다음

inbox = mapi.GetDefaultFolder(0) 

나는 당신이 또한

mapi = outlook.Session 

대신 GetNameSpace 사용할 수 있다고 생각합니다.

+0

나는 당신의 제안을 시도하고 이것을 한 후에 얻는 두 번째 오류로 나의 질문을 편집했다. – DarthOpto

+0

@DarthOpto 어떻게 알 수 있습니까 0은 유효한 값입니다 – Schollii

+0

고마워요,이 작동, 분명히받은 편지함은 0이 아니지만 6 – DarthOpto

관련 문제