1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
| Sub VulAgenda(myColumns, myWeeks)
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim rowCount As Integer
Dim myStringPos As Integer
Dim myStartColumn As Integer
Dim myEndColumn As Integer
Dim olApp As Outlook.Application
Dim olApt As AppointmentItem
Dim blnCreated As Boolean
Dim daStart, daEnd As Date
Dim oCalendar As Outlook.Folder
Dim oItems As Outlook.Items
Dim oItemsInDateRange As Outlook.Items
Dim oFinalItems As Outlook.Items
Dim oAppt As Outlook.AppointmentItem
Dim strRestriction As String
Dim myNamespace As Outlook.Namespace
Dim myRecipient As Outlook.Recipient
Dim myRecipientName As String
Dim myRowPosition As Integer
On Error GoTo Errhandler
'Check how many columns should be processed
If myColumns = 0 Then
'Process all columns
myStartColumn = 8 '1e persoon, kolom H
myEndColumn = 24 'laatste persoon kolom X
Else
'Process only selected column
myStartColumn = myColumns
myEndColumn = myColumns
End If
'Check how many weeks should be processed
'And set start position of weeks to be processed
If myWeeks = 1 Then
j = ActiveCell.Row
For i = j - 10 To j
If Left(Cells(i, 1).Value, 4) = "Week" Then
myRowPosition = i
End If
Next i
'Debug.Print myRowPosition
Else
j = 5
Do
j = j + 1
If j > 700 Then
Exit Do
End If
Loop Until Date = Cells(j, 1)
For i = j - 10 To j
If Left(Cells(i, 1).Value, 4) = "Week" Then
myRowPosition = i
End If
Next i
'Debug.Print myRowPosition
End If
For j = myStartColumn To myEndColumn
Debug.Print "Person to process: " & Cells(6, j).Value
'Get correct name, also when charachter ( is present
For myStringPos = 1 To Len(Cells(6, j))
If Mid(Cells(6, j).Value, myStringPos, 1) = "(" Then
myRecipientName = Left(Cells(6, j).Value, myStringPos - 2)
myStringPos = Len(Cells(6, j))
Else
myRecipientName = Cells(6, j).Value
End If
Next myStringPos
Debug.Print myRecipientName
Set olApp = GetObject(, "Outlook.Application")
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
blnCreated = True
Err.Clear
Else
blnCreated = False
End If
Set myNamespace = olApp.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient(myRecipientName)
myRecipient.Resolve
If myRecipient.Resolved Then
For i = 1 To (10 * myWeeks + myWeeks)
If Cells(myRowPosition + i, 1).Value <> "" And Left(Cells(myRowPosition + i, 1).Value, 4) <> "Week" Then
Debug.Print "Date to process: " & Cells(myRowPosition + i, 1).Value
'Debug.Print "Lets delete appointments, if present"
'Construct a filter for the appointment to be removed
strRestriction = "[Start] >= '" & Cells(myRowPosition + i, 1).Value & " 00:00' AND [End] <= '" & Cells(myRowPosition + i, 1).Value & " 23:59'"
'Debug.Print strRestriction
'Set oCalendar = Session.GetDefaultFolder(olFolderCalendar)
Set oCalendar = Session.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
Set oItems = oCalendar.Items
oItems.IncludeRecurrences = False
oItems.Sort "[Start]"
' Restrict the Items collection for the filter.
Set oItemsInDateRange = oItems.Restrict(strRestriction)
'Debug.Print oItems.Count
'Debug.Print oItemsInDateRange.Count
'Delete Appointment with correct Category
For rowCount = oItemsInDateRange.Count To 1 Step -1
If TypeName(oItemsInDateRange(rowCount)) = "AppointmentItem" Then
'Debug.Print " " & rowCount, oItemsInDateRange(rowCount).Start, oItemsInDateRange(rowCount).End, oItemsInDateRange(rowCount).Subject
If InStr(1, oItemsInDateRange(rowCount).Categories, "Created by Excel Planning", vbTextCompare) = 1 Then
Debug.Print "Te verwijderen: " & rowCount, oItemsInDateRange(rowCount).Start, oItemsInDateRange(rowCount).End, oItemsInDateRange(rowCount).Subject
oItemsInDateRange(rowCount).Delete
End If
End If
Next rowCount
'Let's create new appointments
If Cells(myRowPosition + i, j).Value <> "" Then
Debug.Print "Planned all day activity: " & Cells(myRowPosition + i, j).Value & " " & Cells(myRowPosition + i + 1, j).Value
Set olApt = oCalendar.Items.Add(Outlook.OlItemType.olAppointmentItem)
With olApt
.Start = Cells(myRowPosition + i, 1).Value + (1 / 24 * 8)
.End = Cells(myRowPosition + i, 1).Value + (1 / 24 * 17)
Select Case Cells(myRowPosition + i, j).Interior.ColorIndex
Case 3
If Cells(myRowPosition + i, j).Value = "Helpdesk" Then
.Subject = Cells(myRowPosition + i, j).Value & " " & Cells(myRowPosition + i + 1, j).Value & " - 8:00"
Else
.Subject = Cells(myRowPosition + i, j).Value & " " & Cells(myRowPosition + i + 1, j).Value
End If
.BusyStatus = olBusy
Case 38
If Cells(myRowPosition + i, j).Value = "Helpdesk" Then
.Subject = Cells(myRowPosition + i, j).Value & " " & Cells(myRowPosition + i + 1, j).Value & " - 17:00"
Else
.Subject = Cells(myRowPosition + i, j).Value & " " & Cells(myRowPosition + i + 1, j).Value
End If
.BusyStatus = olBusy
Case 43
.Subject = Cells(myRowPosition + i, j).Value & " " & Cells(myRowPosition + i + 1, j).Value
.BusyStatus = olOutOfOffice
Case 44
If Cells(myRowPosition + i, j).Value = "Helpdesk" Then
.Subject = Cells(myRowPosition + i, j).Value & " " & Cells(myRowPosition + i + 1, j).Value & " - 2e lijns"
Else
.Subject = Cells(myRowPosition + i, j).Value & " " & Cells(myRowPosition + i + 1, j).Value
End If
.BusyStatus = olBusy
Case Else
.Subject = Cells(myRowPosition + i, j).Value & " " & Cells(myRowPosition + i + 1, j).Value
.BusyStatus = olBusy
End Select
.ReminderSet = False
.Categories = "Created by Excel planning"
If Not Cells(myRowPosition + i, j).Comment Is Nothing And Cells(myRowPosition + i + 1, j).Comment Is Nothing Then
.Body = Cells(myRowPosition + i, j).Comment.Text
End If
If Cells(myRowPosition + i, j).Comment Is Nothing And Not Cells(myRowPosition + i + 1, j).Comment Is Nothing Then
.Body = Cells(myRowPosition + i + 1, j).Comment.Text
End If
If Not Cells(myRowPosition + i, j).Comment Is Nothing And Not Cells(myRowPosition + i + 1, j).Comment Is Nothing Then
.Body = Cells(myRowPosition + i, j).Comment.Text & vbLf & vbLf & Cells(myRowPosition + i + 1, j).Comment.Text
End If
.Save
End With
Set olApt = Nothing
End If
Set oCalendar = Nothing
Set oItems = Nothing
Set oItemsInDateRange = Nothing
Set oFinalItems = Nothing
Set olApt = Nothing
Debug.Print
End If
Next i
Set olApp = Nothing
Set myNamespace = Nothing
Set myRecipient = Nothing
Else
Debug.Print "Geen MAPI"
'Exit Sub
End If
Next j
Exit Sub
Errhandler:
MsgBox "Er gaat iets fout." & vbCr & "Waarschijnlijk geen rechten op Calendar van " & Cells(6, j).Value
End Sub |