vbaoutlook

Change appointment subject associated with incoming invite mail


I made the following script to rename incoming mails as a rule in Outlook:

Sub RenameMails(MyMail As MailItem)
    Dim strID As String
    Dim objMail As Outlook.MailItem

    strID = MyMail.EntryID
    Set objMail = Application.Session.GetItemFromID(strID)
    If Left(objMail.Subject, 4) = "FW: " Then
        objMail.Subject = Right(objMail.Subject, Len(objMail.Subject) - 4)
        objMail.Subject = "Test: " & objMail.Subject
        objMail.Save
    End If

    Set objMail = Nothing
End Sub

This works for regular incoming mails, but if the mail is an invite to a Teams Meeting, it doesn't change the subject.
It is also not possible to rename the invite mail in Outlook, but it is possible to rename the appointment in the calendar.

How do I rename the appointment that is associated with the invite mail?


Solution

  • I made it work like this:

    Sub RenameMails(objMail As Object)
        Dim myAppt As Outlook.AppointmentItem
        
        If (Left(objMail.Subject, 4) = "FW: ") Then
            objMail.Subject = Right(objMail.Subject, Len(objMail.Subject) - 4)
            objMail.Subject = "Test: " & objMail.Subject
            objMail.Save
        End If
        
        If (objMail.Class = olMeetingRequest) Then
            Set myAppt = objMail.GetAssociatedAppointment(True)
            
            If (Left(myAppt.Subject, 4) = "FW: ") Then
                myAppt.Subject = Right(myAppt.Subject, Len(myAppt.Subject) - 4)
                myAppt.Subject = "Test: " & myAppt.Subject
                myAppt.Save
            End If
        End If
    End Sub