2

I'm trying to retrieving free/busy status from outlook calender for particular person using python language.

here is my code for it.

import win32com.client

obj_outlook = win32com.client.Dispatch('Outlook.Application')
obj_Namespace = obj_outlook.GetNamespace("MAPI")
obj_Recipient = obj_Namespace.CreateRecipient("someone@domain.com")
str_Free_Busy_Data = obj_Recipient.FreeBusy("11-11-2013", 11)
print str_Free_Busy_Data

but I'm getting an error:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    str_Free_Busy_Data = obj_Recipient.FreeBusy("11-11-2013", 11)
  File "<COMObject CreateRecipient>", line 4, in FreeBusy
TypeError: an integer is required

So my question is Recipient.FreeBusy() method takes two mandatory arguments, Start Date and duration. Here 11 is the duration, which is an Integer. So why python is not able to identify the integer argument here and returning an TypeError.

Please help me in case I have done anything wrong (I'm still a newbie in python world).

Thanks in advance.

  • Could you try passing a zero for the third argument? – abhi Nov 26 '13 at 16:51
  • @abhi - I tried passing zero for third argument but that didn't work. I also tried None as third argument but ended up with same error. Anyways third argument is optional, so it does not matter what we pass there. – Anirudha Patil Nov 27 '13 at 03:00
  • I would make a guess here as to putting int(11) around the 11 and see if that makes any difference. – abhi Nov 27 '13 at 15:54
  • Nope..still not working...Do you have any other alternative?.. – Anirudha Patil Nov 27 '13 at 16:28
  • Yes. I posted my answer. I tried this out in PYcharm. It works. The only issue I have is now interpreting the string being returned by the method. – abhi Nov 27 '13 at 17:23
  • @abhi - thanks abhi for your answer. really appreciate your work. I never thought of that one. After getting the free/busy status, which actually returns the value in binary format instead of string, I'm going to use 0 (which signify 'Free' and 1 signifies 'busy') to book the resource. Again thanks for solving my problem :) – Anirudha Patil Nov 28 '13 at 03:12

2 Answers2

3

I looked up the method in MSDN.

http://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.outlook.recipient.freebusy(v=office.12).aspx

The syntax for the method takes 3 arguments.

string FreeBusy(
DateTime Start,
int MinPerChar,
Object CompleteFormat

)

The issue is that you're passing a string to the DateTime parameter. Instead you need to import the datetime library in your code and use a date parameter.

So, at the start of your code, try this.

import datetime

#Then declare the my_date variable as datetime.date.

my_date = datetime.date(2013,11,23)
str_Free_Busy_Data = obj_Recipient.FreeBusy(my_date, 11)
abhi
  • 3,082
  • 6
  • 47
  • 73
2

The first parameter to FreeBusy is a Date object. Pywin won't convert a string into a Date , but it can convert a pywintypes.Time object, or an integer representing the number of seconds since the Unix epoch. Hence the error: When the first argument is implicitly converted to a Time, the constructor complains that it needs an integer.

#start date: 12/31/1969 7:00:00 PM
str_Free_Busy_Data = obj_Recipient.FreeBusy(0, 11)

There are a number of ways to get the Unix timestamp from a date. See Convert python datetime to epoch with strftime.

Community
  • 1
  • 1
Kevin
  • 74,910
  • 12
  • 133
  • 166