Python - send meeting but do not show in my callendar

618 Views Asked by At

I was tasked with fiding way how to add approved vacations to calendar from database. So I made this simple code:

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application")

def sendMeeting():    
  appt = outlook.CreateItem(1) # AppointmentItem
  appt.Start = "2021-12-09 10:10" # yyyy-MM-dd hh:mm
  appt.Subject = "Test test"
  appt.Duration = 60
  appt.Location = "Out of office"
  appt.MeetingStatus = 1 # 1 - olMeeting; Changing the appointment to meeting. Only after changing the meeting status recipients can be added
  appt.Organizer = "[email protected]"

  appt.Recipients.Add("[email protected]") # Don't end ; as delimiter

  appt.Save()
  appt.Send()
  print("sent")


sendMeeting()

This is all well, but problem is that if I sent the meeting It also appear in my calendar. So If I was to create it for 100 users my callendar would be spammed.

Also I can't connect directly to server and creating something more complex like this: Sending Meeting Invitations With Python. This is not desirable, because I presume, that I would need get login and password for all the users.

So is there some simple way to do it? Meaby just delete it afterwards or outlook parameter, that I can disable it with?

1

There are 1 best solutions below

0
On BEST ANSWER

There is no way to get this working by automating Outlook locally.

Everything you do with the Outlook object model is related to the local accounts only. If you need to modify the remote calendar by adding appointments or meeting without customizing yours, consider using the EWS, Outlook REST API or Graph API if you use Exchange accounts or O365. See Explore the EWS Managed API, EWS, and web services in Exchange and One Outlook REST API - your favorite platform - 400+ million users for more information. Graph API is most reliable way because other technologies are going to die in near future.

Also you may consider delegating access to other accounts and use the NameSpace.GetSharedDefaultFolder method which 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). In that case you will be able to access calendar folders of users and add appointments directly there without sending anything. Here is the sample VBA code which illustrates the sequence of OOM calls:

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

I know the question is in Python, but the Outlook object model is common for all kind of programming languages, so hopefully it will not a problem to understand the OOM calls.