I’m receiving an error while I’m trying to save a code that gets an outlook’s mail receivedTime. But I don’t know what possibly could done wrong. See the error below:
JavaScript
x
3
1
Exception has occurred: TypeError
2
Excel does not support timezones in datetimes. The tzinfo in the datetime/time object must be set to None.
3
Can anyone help me?
The code:
JavaScript
1
33
33
1
from datetime import datetime
2
from operator import index
3
from typing import Pattern
4
import win32com.client
5
import openpyxl
6
from openpyxl import Workbook
7
from openpyxl.styles import Font, Color, PatternFill, fills, Border, Side
8
9
10
outlook = win32com.client.Dispatch("outlook.application")
11
mapi = outlook.GetNamespace("MAPI")
12
account = [i for i in mapi.Accounts][0]
13
inbox = mapi.GetDefaultFolder(6)
14
test_folder = inbox.Folders["TravelAlertReport"]
15
items = test_folder.Items
16
17
wb= Workbook()
18
planilha = wb.worksheets[0]
19
ws=wb["Sheet"]
20
21
#Header
22
23
list_header = ["Status", "Level", "Location", "Category", "Date", "Month", "Year", "Relevant?(Yes/No)", "Justification", "Used by TST", "Learning Required?"]
24
25
#Rows' content
26
for index,item in enumerate(items):
27
28
#Time
29
date_time = item.ReceivedTime
30
ws.cell(row=index+2, column = 5).value = date_time
31
32
wb.save("Report.xlsx")
33
Advertisement
Answer
The MailItem.ReceivedTime property returns a Date indicating the date and time at which the item was received.
Try to use any other date object in the code to see whether any difference exists, for example:
JavaScript
1
6
1
'Insert Today's Date
2
Range("A1").Value = Date
3
4
'Insert A Date (mm/dd/yyyy)
5
Range("B1") = #1/28/2019#
6
If that code works then you need to format the date object in the way which Excel understands. The Format function can help with that.