2017-02-21 8 views
0

我想下载的Windows 7从Outlook 2013附件运行下载附件我写了下面的它的功能:“UnboundLocalError”从Outlook 2013时使用不同的参数

def attach(subject,name): 
    print "Execution begin" 
    outlook = Dispatch("Outlook.Application").GetNamespace("MAPI") 
    inbox = outlook.GetDefaultFolder("6") 
    all_inbox = inbox.Items 
    val_date = date.date.today() 

    print "accessing parameters" 
    sub_today = subject 
    att_today = name 

    for msg in all_inbox: 
     if msg.Subject == sub_today: 
     print "sub_today" 
     break 
    for attachment in msg.Attachments: 
     if attachment.FileName == att_today: 
     print "att_today" 
     print attachment 
     break 

    repository='C:\\Users\\tanmay.shrivastava\\Desktop\\Dashboard' 
    print "repository" 
    attachment.SaveAsFile(repository + '\\'+att_today) 
    print "saved" 
    print "Execution completed" 

我叫函数有两个不同的列表:列表1和列表2.

list1=['Hi','cr.txt'] 
list2=['abc Requirements','cr.txt'] 

当我使用list1调用它时代码正在工作。

attach(list1[0],list1[1]) 

但是当我从列表2参数调用它,它会显示错误

attach(list2[0],list2[1]) 

UnboundLocalError       Traceback (most recent call last) 
<ipython-input-12-4238cd3c2efd> in <module>() 
----> 1 attach(list2[0],list2[1]) 

<ipython-input-2-3bb862eb2bb6> in attach(subject, name) 
    26  repository='C:\\Users\\tanmay.shrivastava\\Desktop\\Dashboard' 
    27  print "repository" 
---> 28  attachment.SaveAsFile(repository + '\\'+att_today) 
    29  print "saved" 
    30  print "Execution completed" 

UnboundLocalError: local variable 'attachment' referenced before assignment 

我不知道是什么原因造成的。有人可以帮我吗?

+0

我的猜测是'msg.Attachments'是空的,因此'attachment'从未定义。 –

+0

@AlexFung我通过发送电子邮件给自己并附带必要的附件进行测试。如果邮件不存在,那么我想它会引发一个“AttributeError” –

+0

所以我的解决方案不能避免错误? –

回答

0

你有错误的原因是没有找到味精或附件。如在问题中提到

这不会引发错误:

def attach(subject,name): 
    print "Execution begin" 
    outlook = Dispatch("Outlook.Application").GetNamespace("MAPI") 
    inbox = outlook.GetDefaultFolder("6") 
    all_inbox = inbox.Items 
    val_date = date.date.today() 

    print "accessing parameters" 

    for msg in all_inbox: 
     if msg.Subject == subject: 
      print "sub_today" 

      for attachment in msg.Attachments: 
       if attachment.FileName == name: 
        print "att_today" 
        print attachment 

        repository='C:\\Users\\tanmay.shrivastava\\Desktop\\Dashboard' 
        print "repository" 
        attachment.SaveAsFile(repository + '\\'+name) 
        print "saved" 
        print "Execution completed" 
        return True 
+0

我用自己的电子邮件发送了所需的邮件主题所需的文件,但它导致了错误。我会检查这是否有效。 –

+0

让我知道它是怎么回事。 –

+0

它仍然显示了'UnboundLocalError:':assignment' –

相关问题