PDA

View Full Version : Outlook VBA Decline Meeting



Fettertiger
05-23-2022, 01:06 AM
Dears,

while I am quite proficient coding in VBA in Excel, I am still a noob when it comes to Outlook. Please apologize, if I am asking something which is obvious to you.

I regularily receive meeting invites from specific users, which are blocking my calendar. I would therefore like to setup a VBA macro to automatically decline the meeting request, if it was sent by a specific user and a specific word is mentioned in the subject. My current Macro works "sort of", as it does not loop through all invites. Even if an invite from the specific user is found, the macro often does not get the corresponding calendar entry when running the
Set myAppt = myMtgReq.GetAssociatedAppointment(False) line.

Does anyone have a better ideo, or know what would need to be changed in my code below?


Sub DeclineSpamMeetings()
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim myMtgReq As Outlook.MeetingItem
Dim myAppt As Outlook.AppointmentItem
Dim myMtg As Outlook.MeetingItem
Dim SenderEmail As String
Dim i As Integer


If DisableEvents = False Then
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
DisableEvents = True
For i = 1 To 10
Set myMtgReq = myFolder.Items.Find("[MessageClass] = 'IPM.Schedule.Meeting.Request'")
If TypeName(myMtgReq) <> "Nothing" Then
If InStr(1, UCase(myMtgReq.SenderEmailAddress), "DOE.J") > 0 OrInStr(1, UCase(myMtgReq.SenderEmailAddress), "MUSTERMANN.F") > 0 Then
SenderEmail = myMtgReq.SenderEmailAddress

Set myAppt = myMtgReq.GetAssociatedAppointment(False)
If Not myAppt Is Nothing Then
If InStr(1, myAppt.Subject, "Project XY") > 0 Then
Set myMtg = myAppt.Respond(olMeetingDeclined, True, False)
myMtg.Send
myMtgReq.Delete
MsgBox "Auto-Declined meeting (" & SenderEmail & ")"
End If
End if
End If
End If
Next
DisableEvents = False
End If
Set myAppt = Nothing
End Sub

Thanks for your help

Theo