Última atividade 1753357397

Revisão bea1cce4a432243de122dec12b9f73e368e57502

gistfile1.txt Bruto
1' Serien-Kalendereinträge aus Excel erstellen mit Serienlogik, Löschung alter Einträge, Logging, Laufzeitmessung, Zusammenfassung und Simulationsmodus
2
3Sub SerienKalendereintraegeErstellen()
4 ' Benutzer wird gefragt, ob Simulation gewünscht ist
5 Dim userResponse As VbMsgBoxResult
6 userResponse = MsgBox("Möchtest du eine Simulation durchführen (keine Einträge werden erstellt oder gelöscht)?", vbYesNo + vbQuestion, "Simulationsmodus")
7 Dim DRYRUN As Boolean
8 DRYRUN = (userResponse = vbYes) ' True = Simulation, False = Echtbetrieb
9
10 Dim olApp As Object, olNS As Object
11 Dim allCalendars As Collection, selectedCalendar As Object
12 Dim calendarNames() As String, selectedIndex As Variant
13 Dim olItems As Object, ws As Worksheet
14 Dim lastRow As Long, i As Long, j As Long, erstelltCount As Long, serienCount As Long, geloeschtCount As Long, uebersprungenCount As Long
15 Dim eintraege As Collection
16 Dim datum As Date, startzeit As Date, endzeit As Date
17 Dim produkt As String, hinweis As String, veranstaltungsnummer As String
18 Dim titel As String, ort As String
19 Dim alreadyProcessed() As Boolean
20 Dim startTime As Double
21 Dim logSheet As Worksheet
22
23 startTime = Timer
24
25 On Error Resume Next
26 Set olApp = GetObject(, "Outlook.Application")
27 If olApp Is Nothing Then Set olApp = CreateObject("Outlook.Application")
28 On Error GoTo 0
29 If olApp Is Nothing Then
30 MsgBox "Outlook konnte nicht gestartet werden.", vbCritical
31 Exit Sub
32 End If
33 Set olNS = olApp.GetNamespace("MAPI")
34 Set allCalendars = New Collection
35
36 Dim store As Object, folder As Object
37 For Each store In olNS.Folders
38 For Each folder In store.Folders
39 If folder.DefaultItemType = 1 Then allCalendars.Add folder
40 Next folder
41 Next store
42
43 ReDim calendarNames(1 To allCalendars.Count)
44 For i = 1 To allCalendars.Count
45 calendarNames(i) = allCalendars(i).Parent.Name & " – " & allCalendars(i).Name
46 Next i
47 selectedIndex = Application.InputBox("Wähle den Zielkalender aus (Zahl):" & vbCrLf & Join(calendarNames, vbCrLf), "Kalenderauswahl", Type:=1)
48 If Not IsNumeric(selectedIndex) Or selectedIndex < 1 Or selectedIndex > allCalendars.Count Then Exit Sub
49
50 Set selectedCalendar = allCalendars(selectedIndex)
51 MsgBox "Ausgewählter Kalender: " & selectedCalendar.Name, vbInformation, "Kalenderbestätigung"
52 Set olItems = selectedCalendar.Items
53 olItems.Sort "[Start]"
54 olItems.IncludeRecurrences = True
55 Dim gefilterteItems As Object
56 Set gefilterteItems = olItems
57
58 Debug.Print "Kalender geladen: " & selectedCalendar.Name & ", " & gefilterteItems.Count & " Elemente gefunden"
59 Set ws = ThisWorkbook.Sheets(1)
60 lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
61 ReDim alreadyProcessed(1 To lastRow)
62
63 Set eintraege = New Collection
64
65 For i = 2 To lastRow
66 Debug.Print "---"
67 Debug.Print "Verarbeite Zeile: " & i
68 Debug.Print "Datum: " & ws.Cells(i, "B").Text
69 Debug.Print "Startzeit: " & ws.Cells(i, "C").Text
70 Debug.Print "Endzeit: " & ws.Cells(i, "D").Text
71 Debug.Print "Produkt: " & ws.Cells(i, "G").Text
72 Debug.Print "Hinweis für Trainer: " & ws.Cells(i, "L").Text
73 Debug.Print "Veranstaltungsnummer: " & ws.Cells(i, "F").Text
74 Debug.Print "Ort: " & ws.Cells(i, "J").Text
75 If alreadyProcessed(i) Then GoTo Weiter
76 If Not IsDate(ws.Cells(i, "B").Value) Then
77 ws.Cells(i, "M").Value = "übersprungen (Datum fehlt)"
78 uebersprungenCount = uebersprungenCount + 1
79 GoTo Weiter
80 End If
81 If ws.Cells(i, "C").Value = "" Then
82 ws.Cells(i, "M").Value = "übersprungen (Startzeit fehlt)"
83 uebersprungenCount = uebersprungenCount + 1
84 GoTo Weiter
85 End If
86 If ws.Cells(i, "G").Value = "" Then
87 ws.Cells(i, "M").Value = "übersprungen (kein Produkt)"
88 uebersprungenCount = uebersprungenCount + 1
89 GoTo Weiter
90 End If
91
92 datum = DateValue(ws.Cells(i, "B").Value)
93 If IsDate(ws.Cells(i, "C").Value) Then
94 startzeit = TimeValue(ws.Cells(i, "C").Value)
95 Else
96 ws.Cells(i, "M").Value = "übersprungen (ungültige Startzeit)"
97 GoTo Weiter
98 End If
99 If IsDate(ws.Cells(i, "D").Value) Then
100 endzeit = TimeValue(ws.Cells(i, "D").Value)
101 Else
102 ws.Cells(i, "M").Value = "übersprungen (ungültige Endzeit)"
103 GoTo Weiter
104 End If
105
106 produkt = Trim(ws.Cells(i, "G").Value)
107 veranstaltungsnummer = Trim(ws.Cells(i, "F").Value)
108 hinweis = Trim(ws.Cells(i, "L").Value)
109 ort = Trim(ws.Cells(i, "J").Value)
110
111 If InStr(1, hinweis, "Lehrgang: ", vbTextCompare) > 0 Then
112 hinweis = Mid(hinweis, InStr(1, hinweis, "Lehrgang: ", vbTextCompare) + 10)
113 End If
114 titel = produkt & " - " & hinweis
115 If titel = " - " Or titel = "-" Then
116 ws.Cells(i, "M").Value = "übersprungen (kein gültiger Titel)"
117 GoTo Weiter
118 End If
119
120 ' Duplikatprüfung: Wenn in Outlook bereits ein Termin mit Datum und Veranstaltungsnummer vorhanden ist
121 Dim skip As Boolean: skip = False
122 If Not DRYRUN Then
123 Dim existingItem As Object, itemDate As Date
124 For Each existingItem In gefilterteItems
125 If existingItem.Class = 26 Then
126 itemDate = Int(existingItem.Start)
127 If itemDate = datum Then
128 If InStr(existingItem.Body, veranstaltungsnummer) > 0 Then
129 If existingItem.Subject <> titel Then
130 Dim msg As VbMsgBoxResult
131 msg = MsgBox("Ein Eintrag mit gleicher Veranstaltungsnummer aber abweichendem Titel existiert: " & existingItem.Subject & vbCrLf & _
132 "Neuer Titel wäre: " & titel & vbCrLf & "Trotzdem ersetzen?", vbYesNo + vbExclamation)
133 If msg = vbNo Then skip = True
134 Else
135 skip = True
136 End If
137 End If
138 End If
139 End If
140 Next existingItem
141 End If
142 If skip Then
143 ' Bestehenden Termin ggf. löschen
144 If Not DRYRUN Then
145 For Each existingItem In gefilterteItems
146 If existingItem.Class = 26 And Int(existingItem.Start) = datum Then
147 If InStr(existingItem.Body, veranstaltungsnummer) > 0 Then
148 existingItem.Delete
149 geloeschtCount = geloeschtCount + 1
150 Exit For
151 End If
152 End If
153 Next
154 End If
155 ws.Cells(i, "M").Value = "ersetzt (gelöscht + neu)"
156 GoTo Weiter
157 End If
158
159 ' Erstellung des Outlook-Termins oder Simulation
160 If DRYRUN Then
161 ws.Cells(i, "M").Value = "(Simulation) " & titel
162 ws.Cells(i, "M").Interior.Color = RGB(200, 200, 200)
163 Else
164 Dim appt As Object
165 Set appt = olApp.CreateItem(1)
166 With appt
167 .Start = datum + startzeit
168 .End = datum + endzeit
169 .Subject = titel
170 .Location = ort
171 .Body = veranstaltungsnummer
172 .ReminderSet = False
173 .BusyStatus = 2
174 .Save
175 End With
176 ws.Cells(i, "M").Value = titel
177 erstelltCount = erstelltCount + 1
178 End If
179 alreadyProcessed(i) = True
180
181 ' Serientermin-Erkennung: gleiche Veranstaltungsnummer an aufeinanderfolgenden Werktagen
182 Dim serieEndRow As Long: serieEndRow = i
183 Do While serieEndRow + 1 <= lastRow And Not alreadyProcessed(serieEndRow + 1)
184 Dim nextDatum As Date
185 If Not IsDate(ws.Cells(serieEndRow + 1, "B").Value) Then Exit Do
186 nextDatum = DateValue(ws.Cells(serieEndRow + 1, "B").Value)
187 If ws.Cells(serieEndRow + 1, "F").Value <> veranstaltungsnummer Then Exit Do
188 If Weekday(datum, vbMonday) = 5 Then ' Freitag
189 If DateDiff("d", datum, nextDatum) <> 3 Then Exit Do ' Freitag → Montag
190 Else
191 If DateDiff("d", datum, nextDatum) <> 1 Then Exit Do
192 End If
193 If ws.Cells(serieEndRow + 1, "G").Value = "" Then Exit Do
194 serieEndRow = serieEndRow + 1
195 Loop
196
197 If serieEndRow > i Then
198 ' Serien-Eintrag
199 If Not DRYRUN Then
200 Dim recurrenceAppt As Object
201 Set recurrenceAppt = olApp.CreateItem(1)
202 With recurrenceAppt
203 .Start = datum + startzeit
204 .End = datum + endzeit
205 .Subject = titel
206 .Location = ort
207 .Body = veranstaltungsnummer
208 .ReminderSet = False
209 .BusyStatus = 2
210 .Save
211 Dim pattern
212 Set pattern = .GetRecurrencePattern
213 pattern.RecurrenceType = 1 ' daily
214 pattern.PatternStartDate = datum
215 pattern.Occurrences = serieEndRow - i + 1
216 .Save
217 End With
218 For j = i To serieEndRow
219 alreadyProcessed(j) = True
220 ws.Cells(j, "M").Value = titel & " (Serie)"
221 Next j
222 serienCount = serienCount + 1
223 GoTo Weiter
224 Else
225 For j = i To serieEndRow
226 alreadyProcessed(j) = True
227 ws.Cells(j, "M").Value = "(Simulation) " & titel & " (Serie)"
228 ws.Cells(j, "M").Interior.Color = RGB(180, 180, 180)
229 Next j
230 serienCount = serienCount + 1
231 GoTo Weiter
232 End If
233 End If
234
235Weiter:
236 Next i
237
238 Dim elapsedTime As Double
239 elapsedTime = Timer - startTime
240 MsgBox "Fertig. " & erstelltCount & " Termine erstellt, " & serienCount & " Serientermine erstellt, " & geloeschtCount & " ersetzt, " & uebersprungenCount & " übersprungen." & vbCrLf & _
241 "Laufzeit: " & Format(elapsedTime, "0.00") & " Sekunden", vbInformation
242End Sub
243