Giuseppe Bonavita
Giuseppe Bonavita

Reputation: 349

Python adding hyperlink to Outlook task through win32com

I would like to create an hyperlink in the body of a task created through win32com.

This is my code so far:

outlook = win32com.client.Dispatch("Outlook.Application")
outlook_task_item = 3
recipient = "[email protected]"
task = outlook.CreateItem(outlook_task_item)
task.Subject = "hello world"
task.Body = "please update the file here"
task.DueDate = dt.datetime.today()
task.ReminderTime = dt.datetime.today()
task.ReminderSet = True
task.Save()

I have tried to set the property task.HTMLBody but I get the error:

AttributeError: Property 'CreateItem.HTMLBody' can not be set.

I have also tried

task.Body = "Here is the <a href='http://www.python.org'>link</a> I need"

but I am not getting a proper hyperlink.

However if I create a task front end in Outlook, I am able to add hyperlinks.

Upvotes: 2

Views: 4956

Answers (2)

Hari kumar
Hari kumar

Reputation: 51

You can also try:

task.HTMLBody = "Here is the <a href='http://www.python.org'>link</a> I need"

this will overwrite data in 'task.Body' to the HTML format provides in 'task.HTMLBody'

so whichever (Body or HTMLBody) is last will be taken as the Body of the mail.

Upvotes: 5

fuglede
fuglede

Reputation: 18211

Tasks do not support HTML. Instead, you have to provide RTF.

You can investigate -- but not set -- the RTF of a given task through task.RTFBody (and task.RTFBody.obj to get a convenient view of it). To use RTF in the body of a task, simply use the task.Body property; setting this to a byte array containing RTF will automatically use that RTF in the body. Concretely, to get the body you want, you could let

task.Body = rb'{\rtf1{Here is the }{\field{\*\fldinst { HYPERLINK "https://www.python.org" }}{\fldrslt {link}}}{ I need}}'

Upvotes: 2

Related Questions