自升级到Office 365以来,Python脚本不再在Outlook中附加文件

时间:2018-10-17 17:33:23

标签: python-2.7 outlook office365

我有一个脚本已在Outlook 2010中使用了很长时间。我们的组织刚刚将我的计算机更新为Office 365(Outlook 2016),该脚本不再附加pdf文件。

for arg in sys.argv:
    baseDir = arg
arcpy.AddMessage("File = " + baseDir)

csvFile = (baseDir + "EmailAnalysis.csv")
# Get Access exported file

arcpy.AddMessage ("csv = " + csvFile)

csvRead = csv.reader(open(csvFile, "r"))
for row in csvRead:
    recipientName, txtRecipient, projectName, cntyList, requestType, DSA_Loc = row

# Ensure Access data format ready to generate list

requestType = split_line(requestType)


# Create the body of the email
if "Analysis" in requestType:
    txtSubject = "Data Analysis Request Response - " + projectName
    txtBody = "Dear " + recipientName + ":<br /><br />Here is the response to your request for near " + projectName + ". Please let me know if you have any questions or require additional information."
    txtAttach = ("\"" + baseDir + "Results\\" + projectName + " - Analysis Response.pdf\"")
else:
    arcpy.AddMessage("exiting script....")
    sys.exit()

# Call up Outlook email with headings and body of text
outlook = EnsureDispatch("Outlook.Application")
newMail = outlook.CreateItem(constants.olMailItem)

newMail.Subject = txtSubject
newMail.GetInspector
index = newMail.HTMLBody.find('>', newMail.HTMLBody.find('<body'))
newMail.HTMLBody = newMail.HTMLBody[:index + 1] + txtBody + newMail.HTMLBody[index + 1:]
newMail.To = txtRecipient
attachment1 = txtAttach
arcpy.AddMessage ("Atttachment 1  = " + attachment1)
newMail.Attachments.Add(attachment1)
grp1 = newMail.Recipients.Add("Eastern Region Data Request")
grp1.Type = constants.olCC

newMail.Recipients.ResolveAll()
newMail.Display(False)

它在“ newMail.Attachments.Add(attachment1)”这一行炸弹

我收到以下错误消息:

File = V:\Test\
csv = V:\Test\EmailAnalysis.csv
Atttachment 1  = "V:\Test\Results\Test Results - Analysis Response.pdf"
Traceback (most recent call last):
  File "V:\Test\Template\Scripts\EmailTest.py", line 82, in <module>
    newMail.Attachments.Add(attachment1)
  File "C:\Python27\ArcGIS10.5\lib\site-packages\win32com\gen_py\00062FFF-0000-0000-C000-000000000046x0x9x6\Attachments.py", line 35, in Add
    , Type, Position, DisplayName)
com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u'File name or directory name is not valid.', None, 0, -2147024773), None)

文件名/路径正确。我已经在互联网上搜索了好几天,却找不到失败的原因。 Outlook中是否有需要修改的我不知道的设置? (顺便说一句,如果我注释掉上面的行,脚本将按预期工作。)

我只输入了直接路径名而不是变量,它起作用了。为什么“ newMail.Attachments.Add(attachment1)”不起作用,但是newMail.Attachments.Add(“ V:\ Test \ Results \ Test Results-Analysis Response.pdf”)起作用呢?

0 个答案:

没有答案