Consulting

Results 1 to 2 of 2

Thread: Run script to move incoming appointments based on date of appointment

  1. #1
    VBAX Newbie
    Joined
    Nov 2015
    Posts
    1
    Location

    Run script to move incoming appointments based on date of appointment

    I receive a lot of meeting requests that clog up my inbox and want to filter them into a folder ("Calendar Archive") so that I can check them just once a day UNLESS the meeting request/update is for today, in which case I'd like to move it to a different folder ("Appointments"). Essentially I want:

    1. Check if item is a meeting request. If not, exit.
    2. Check if meeting request is for today.
    2a. If yes, move to Appointments folder.
    2b. If no, move to Calendar Archive folder.

    I've written some code so that I can create a rule that runs this as a script on all incoming mail but it doesn't seem to work. Any corrections would be much appreciated!

    [VBA]
    Sub CheckApptDate(Item As Outlook.MailItem)

    If Item.MessageClass <> "IPM.Schedule.Meeting.Request" And Item.MessageClass <> "IPM.Schedule.Meeting.Canceled" Then
    Exit Sub
    End If

    Dim myItem As Outlook.AppointmentItem
    Set myItem = Item
    Set ArchiveFolder = Application.GetNamespace("MAPI"). _
    GetDefaultFolder(olFolderInbox).Parent.Folders("Calendar Archive")
    Set MainFolder = Application.GetNamespace("MAPI"). _
    GetDefaultFolder(olFolderInbox).Folders("Appointments")

    If myItem.Start > Date Then
    myItem.Move ArchiveFolder
    Else
    myItem.Move MainFolder
    End If

    End Sub
    [/VBA]

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Pass MeetingItem not Mailitem.

    You will need this ' https://msdn.microsoft.com/en-us/lib.../ff867189.aspx

    The code should look something like this:

    Sub CheckApptDate(Itm As MeetingItem)
    
        Dim myNS As Namespace
        Dim myInbox As Folder
        Dim myAppt As AppointmentItem
        Dim ArchiveFolder As Folder
        Dim MainFolder As Folder
        
        Set myNS = GetNamespace("MAPI")
      
        If Itm.MessageClass = "IPM.Schedule.Meeting.Request" Then
    
            Set myAppt = Itm.GetAssociatedAppointment(True)
            Set myInbox = myNS.GetDefaultFolder(olFolderInbox)
            Set ArchiveFolder = myInbox.Parent.Folders("Calendar Archive")
            Set MainFolder = myInbox.Folders("Appointments")
         
            If myAppt.Start > Date Then
                Itm.Move ArchiveFolder
            Else
                Itm.Move MainFolder
            End If
            
        End If
        
    ExitRoutine:
        Set myAppt = Nothing
        Set myInbox = Nothing
        Set ArchiveFolder = Nothing
        Set MainFolder = Nothing
        
    End Sub
    Rules with the Run a Script option often break. If you experience this try changing to ItemAdd.
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •