0

With this python code I can make an outlook event in my personal calendar but I would like to make an event in a shared outlook calendar.

FolderPath of the outlook calendar: \Public Folders - my.email@company.com\All Public Folders\COMPANY\Production Can somebody please help me?

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application")
ns = outlook.GetNamespace("MAPI")

#FolderPath of the outlook calendar: \\Public Folders - my.email@company.com\All Public Folders\COMPANY\Production

calendar_folder = ns.Session.Folders('Public Folders - my.email@company.com').Folders("All Public Folders").Folders("COMPANY").Folders("Production")

def Add_outlook_Event(when,subject):    
  appt = outlook.CreateItem(1) #<-- this works
  #appt = outlook.calendar_folder.CreateItem(1) <-- this does not work
  #appt = calendar_folder.CreateItem(1) <-- this does not work
  appt.Start = when # yyyy-MM-dd hh:mm
  appt.Subject = subject 
  appt.AllDayEvent = True
  appt.Save()
  appt.Send()

Add_outlook_Event("2023-03-10","New Outlook event for Bobby")

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
FrankVE
  • 1
  • 1

1 Answers1

0

First, you need to get a shared calendar folder. The NameSpace.GetSharedDefaultFolder method returns a Folder object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Calendar folder). For example, the following VBA sample shows how to use the GetSharedDefaultFolder method:

Sub ResolveName() 
 Dim myNamespace As Outlook.NameSpace 
 Dim myRecipient As Outlook.Recipient 
 Dim CalendarFolder As Outlook.Folder 
 Set myNamespace = Application.GetNamespace("MAPI")
 Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev")
 myRecipient.Resolve 
 If myRecipient.Resolved Then 
   Call ShowCalendar(myNamespace, myRecipient) 
 End If 
End Sub 
 
Sub ShowCalendar(myNamespace, myRecipient) 
 Dim CalendarFolder As Outlook.Folder 
 Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar) 
 CalendarFolder.Display 
End Sub

Second, to add a new item to the shared folder you can use the Add method in the following way:

items = calendarFolder.Items;
appItem = items.Add(Outlook.OlItemType.olAppointmentItem)

The CreateItem method can also be used for creating new items in Outlook. But in that case you need to move the created item to the target folder by using the Move method. Otherwise, it will be saved to the default folder. Read more about all possible ways of creating new Outlook items in the article which I wrote for the technical blog, see How To: Create a new Outlook Appointment item for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45