エラーコード:AttributeError: <unknown>.ReceivedTime
Q&A
助けてください。下記を実行するとエラーになってしまいます。
outlookの情報をエクセルに保存しようとしています。
エラー箇所:[outlookの情報を取得]の上から3行目 RT = message.ReceivedTime
何をどうすればエラーを回避できるでしょうか。
どなたか宜しくお願い致します。
AttributeError: .ReceivedTime
環境条件
windows 10
python 3.8.3
VSCODE
ライブラリ
from openpyxl import load_workbook
import win32com.client
import datetime
import os
所定フォルダ内の「Book.xlsm」を指定して読み込む
filepath = 'C:/Users/'
wb = load_workbook(filename=filepath)
ws1 = wb["データ"]
集計範囲の取得
startdate = ws1["B2"].value
enddate = ws1["B3"].value
outllokの情報を取得
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
outlookの添付ファイル保存
now = datetime.datetime.now()
foldermake = now.strftime('%Y/%m/%d %H:%M:%S')
foldermake = foldermake.replace("/","-")
foldermake = foldermake.replace(":","-")
foldermake = foldermake.replace(" ","-")
newfolder_path = 'C:/Users/'+foldermake
os.makedirs(newfolder_path)
outlookの情報を取得
a=0
for message in messages:
RT = message.ReceivedTime
hiduketime = datetime.datetime(RT.year, RT.month, RT.day, RT.hour, RT.minute, RT.second)
if startdate <= hiduketime <= enddate:
ws1.cell(row=7+a, column=1).value = a+1
ws1.cell(row=7+a, column=2).value = str(message.Subject)
ws1.cell(row=7+a, column=3).value = hiduketime
ws1.cell(row=7+a, column=4).value = str(message.Sender)
ws1.cell(row=7+a, column=5).value = str(message.body[0:100])
if message.Attachments.Count > 0:
myDate = RT.strftime('%Y/%m/%d %H:%M:%S')
myDate = myDate.replace("/","-")
myDate = myDate.replace(":","-")
myDate = myDate.replace(" ","-")
datefolder_path = newfolder_path + "\" + myDate
os.makedirs(datefolder_path)
for myAttachement in message.Attachments:
print(myAttachement.FileName)
myAttachement.SaveAsFile(datefolder_path + "\" +myAttachement.FileName)
ws1.cell(row=7+a, column=6).value = "有"
else:
ws1.cell(row=7+a, column=6).value = "無"
a=a+1
Outlook_Book2.xlsx」として所定のフォルダに保存
newfilepath = 'C:/Users/'
wb.save(newfilepath)
print("保存しました")