2

Background:

I have a task tracking spread sheet and would like to create a calendar "appointment" everytime a new row is added to the table. There are many instances of different versions of this code floating around out there, so I pieced this together with little true knowledge of VBA.

The Data:

Data is stored in a table (Table1) in Sheet1, which I have renamed "Tracker". It's currently ~600 rows, and ~16 columns. The table is constantly updated with new rows of data.

The Problem:

The macro runs, and loops through the 600+ rows of data, creating an appointment for a row, then overwriting that appointment with the data from the next row. I know it's creating + overwriting b/c I set my calendar view to "list view", and ran the macro...and I can see it cycling through all the different rows, so I know it's looping. So I THINK I need assistance modifying the Private Function's subjectFilter. That said, if I remove the Private Function, it does the same thing.

Right now, the .Subject code is this:

.Subject = Cells(r, 9).Value & " (" & Cells(r, 13).Value & " " & Cells(r, 14).Value & ")"

Although I could simplify it to this if it makes it easier to incorp into the subjectFilter:

.Subject = Cells(r, 9).Value

Questions:

  1. How can I adjust the code so it creates all 600+ appointments?
  2. How do I incorporate my .Subject string into the Private Function's subjectFilter?

Current Code:

Sub SetAppt()

Dim olApp As Outlook.Application 
Dim olApt As AppointmentItem
Dim MySheet As Worksheet

Set MySheet = Worksheets("Tracker")
Set olApp = New Outlook.Application
Set olApt = olApp.CreateItem(olAppointmentItem)

For r = 2 To Cells(Rows.Count,1).End(xlUp).Row

With olApt
       .Start = Cells(r, 2).Value + TimeValue("10:30")
       .Duration = "1"
       .Subject = Cells(r, 9).Value & " (" & Cells(r, 13).Value & " " & Cells(r, 14).Value & ")"
       .Location = Cells(r, 5).Value
       .Body = "Follow up with task lead"
       .BusyStatus = olBusy
       .ReminderMinutesBeforeStart = 60
       .Categories = "Task Reminder"
       .ReminderSet = True
       .Save 

End With
Next

Set olApt = Nothing 
Set olApp = Nothing

End Sub


Private Function Get_Appointment(subject As String) As Outlook.AppointmentItem
'Private Function grabbed from here https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwis6IGw7vXXAhXBneAKHWJ9A7kQFggpMAA&url=https%3A%2F%2Fwww.mrexcel.com%2Fforum%2Fexcel-questions%2F686519-using-vba-macro-post-new-appointments-outlook-but-dont-want-duplicates.html&usg=AOvVaw0vUdR7HN9USe52hrOU2M1V

Dim olCalendarItems As Outlook.Items
Dim subjectFilter As String

'Get calendar items with the specified subject
    
subjectFilter = "[Subject] = '" & subject & "'"
Set olCalendarItems = olCalendarFolder.Items.Restrict(subjectFilter)

If olCalendarItems.Count > 0 Then
    Set Get_Appointment = olCalendarItems.Item(1)
Else
    Set Get_Appointment = Nothing
End If
End Function
Community
  • 1
  • 1
kroy2008
  • 175
  • 1
  • 11
  • It's the *same* - appointment you just modify it multiple times in your loop. You need to create a new appointment each time through. – Tim Williams Dec 06 '17 at 17:20

1 Answers1

1

Use a new appointment object for each row - otherwise you're just creating a single appointment and then updating it repeatedly

Const COL_FLAG As Long = 20 '<< "flag" column
'...
'...
For r = 2 To Cells(Rows.Count, 1).End(xlUp).Row
    'Only create an appointment if not already created....
    If Len(Cells(r, COL_FLAG ).Value)= 0 Then 
    With olApp.CreateItem(olAppointmentItem) '<<< use a new object for each iteration
           .Start = Cells(r, 2).Value + TimeValue("10:30")
           .Duration = "1"
           .Subject = Cells(r, 9).Value & " (" & Cells(r, 13).Value & _
                      " " & Cells(r, 14).Value & ")"
           .Location = Cells(r, 5).Value
           .Body = "Follow up with task lead"
           .BusyStatus = olBusy
           .ReminderMinutesBeforeStart = 60
           .Categories = "Task Reminder"
           .ReminderSet = True
           .Save 
           Cells(r, COL_FLAG ).Value = "Created"
    End With
    End If '<< appt not already created
Next
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Tim - many thanks - all the 600+ items are now created, thanks for the help! I am still having trouble with not creating duplicates. Someone provided the Private Function - but I don't understand how to set the PF's subjectFilter so next time I run the SetAppt macro, only new rows of data create appointments. Any ideas? – kroy2008 Dec 07 '17 at 16:25
  • Is the subject of each appt unique? You'd need some way to match each row of your data against the existing calendar item to see whether they match. Another approach might be to add a flag column in your data sheet which you populate with a value after creating the Outlook item: then only process rows where this flag is empty. – Tim Williams Dec 07 '17 at 17:08
  • Yep, the subjects will all be unique. Subject is generated via the cells values in column 9, 13 and 14, resulting in a string of a Name + ID number + a 2nd ID number. Not sure I follow the flag method - can you elaborate on the VBA code I would have to add? – kroy2008 Dec 07 '17 at 17:53
  • See my edit for suggestions on using a "flag" column. – Tim Williams Dec 07 '17 at 18:32
  • Tim - thank you, I've got it working now. I may modify this to create meeting invitations instead of appointment items, but I think I can figure that out. Thanks for your time! – kroy2008 Dec 07 '17 at 20:21