Microsoft Outlook 365 ProPlus -Macro not working

Category: outlook for developers


bearprecioso on Tue, 08 Aug 2017 14:42:29

I received this macro to detect when a meeting is added to my calendar and give an opportunity to block-off travel time on either side of it.  (create a meeting to run from 1:15 to 2:15, then the script would create an appointment from 1:00 – 1:15 for travel to the meeting and another from 2:15 – 2:30 to return from the meeting.

However, I am just ignorant enough (so not a developer) to not understand why the very first statement receives a syntax error.

Can you help?  Maybe there is a simpler macro?  As I understand it, I need to remove lines 16 and 42 in order for this to work for appointments.  However, I want it to work for both meetings and appointment.  Also, I want to alter it to allow me to choose the amount of time before/after a meeting, as sometimes I need 45 minutes of travel each way.

I am using Microsoft Outlook 365 ProPlus on a network using Windows 7 Professional

Here is what I was given: (I am not asking the originator, as they did not respond to my follow up questions)

Can someone help me with this?

1:Dim WithEvents olkCalendar As Outlook.Items
3:Private Sub Application_Quit()
4:    Set olkCalendar = Nothing
5:End Sub
7:Private Sub Application_Startup()
8:    Set olkCalendar = Session.GetDefaultFolder(olFolderCalendar).Items
9:End Sub
11:Private Sub olkCalendar_ItemAdd(ByVal Item As Object)
12:    Const SCRIPT_NAME = "Schedule Travel Time"
13:    Dim olkTravel1 As Outlook.AppointmentItem, _
14:        olkTravel2 As Outlook.AppointmentItem, _
15:        intMinutes As Integer
16:    If Item.MeetingStatus = olMeeting Then
17:        If msgbox("Do you need to schedule travel time for this meeting?", vbQuestion + vbYesNo, SCRIPT_NAME) = vbYes Then
18:            intMinutes = InputBox("How many minutes each way?", SCRIPT_NAME, 15)
19:            If intMinutes > 0 Then
20:                Set olkTravel1 = Application.CreateItem(olAppointmentItem)
21:                With olkTravel1
22:                    'Edit the subject as desired'
23:                    .Subject = "Travel to Meeting: " & Item.Subject
24:                    .Start = DateAdd("n", intMinutes * -1, Item.Start)
25:                    .End = Item.Start
26:                    .Categories = Item.Categories
27:                    .BusyStatus = olBusy
28:                    .Save
29:                End With
30:                Set olkTravel2 = Application.CreateItem(olAppointmentItem)
31:                With olkTravel2
32:                    'Edit the subject as desired'
33:                    .Subject = "Travel from Meeting: " & Item.Subject
34:                    .Start = Item.End
35:                    .End = DateAdd("n", intMinutes, Item.End)
36:                    .Categories = Item.Categories
37:                    .BusyStatus = olBusy
38:                    .Save
39:                End With
40:            End If
41:        End If
42:    End If
43:    Set olkTravel1 = Nothing
44:    Set olkTravel2 = Nothing
45:End Sub


Eugene Astafiev on Tue, 08 Aug 2017 17:07:14

You can replace the following line of code:

 If Item.MeetingStatus = olMeeting Then

to the following one:

 If Item.Class = olAppointment Then

You may find the Getting Started with VBA in Outlook 2010 article helpful.