-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteSchedule.bas
139 lines (109 loc) · 4.34 KB
/
WriteSchedule.bas
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
Option Explicit
' Outlook VBA that extracts Outlook Calendar items and writes them to file ("C:\Coding\PyCharm\projects\OnlineCalendar\templates\schedule.tsv").
Private Function MinDate(dtDate1 As Date, dtDate2 As Date) As Date
' Return the earlier of two dates.
If dtDate1 < dtDate2 Then
MinDate = dtDate1
Else
MinDate = dtDate2
End If
End Function
Private Function MaxDate(dtDate1 As Date, dtDate2 As Date) As Date
' Return the later of two dates.
If dtDate1 > dtDate2 Then
MaxDate = dtDate1
Else
MaxDate = dtDate2
End If
End Function
Private Function GetCalendarObject(ParentFolder As String, SubFolder As String) As Outlook.Folder
' Outlook folders.
Dim oFolder As Outlook.Folder
Dim oSubFolder As Outlook.Folder
Dim oCalendar As Outlook.Folder
Dim FolderPath As String
FolderPath = "\\" & ParentFolder & "\" & SubFolder
' Loop over all top-level folders.
For Each oFolder In Application.Session.Folders
If oFolder.Name = ParentFolder Then
' Loop over subfolders.
For Each oSubFolder In oFolder.Folders
If oSubFolder.Name = SubFolder And oSubFolder.DefaultItemType = olAppointmentItem Then
' Calendar folders only.
If oSubFolder.FolderPath = FolderPath Then
' Get object whose folder path is the desired folder path.
Set GetCalendarObject = oSubFolder
Exit For
End If
End If
Next
End If
Next
If GetCalendarObject Is Nothing Then
MsgBox "Failed to find object for folder path " + FolderPath
End If
End Function
Public Sub WriteSchedule()
' Get Outlook appointments.
Dim oItems As Outlook.Items
Dim oItemsForExport As Outlook.Items
Dim oItem As Outlook.AppointmentItem
' Select appointments for each day in the next 10 days.
Dim dtToday As Date
'Dim dtDayStartTime As Date
Dim dtRangeStart As Date
Dim dtRangeEnd As Date
Dim strRestriction As String
' My usual working hours.
'Dim dtNormalDayStartTime As Date
' Appointment start and end times.
Dim dtItemStartTime As Date
Dim dtItemEndTime As Date
' For working with files.
Dim fso As Object
Dim oFile As Object
' Date/Time format
Dim dtFormat As String
Dim num_days As Integer
Dim output_file As String
Dim WshShell
output_file = "C:\Coding\PyCharm\projects\OnlineCalendar\templates\schedule.tsv"
num_days = 10
' Initialize Globals and Constants
'dtNormalDayStartTime = 9 / 24 ' 9:00am
dtFormat = "yyyy/mm/dd hh:mm:ss"
Set oItems = GetCalendarObject("iCloud", "Calendar").Items
oItems.IncludeRecurrences = True
oItems.Sort "[Start]"
' Define date/time range to get.
'dtDayStartTime = MaxDate(dtNormalDayStartTime, Hour(Now) / 24)
dtToday = Int(Date)
'dtRangeStart = dtToday + dtDayStartTime
dtRangeStart = dtToday + Hour(Now) / 24
dtRangeEnd = dtToday + num_days - (1 / 60 / 24) ' 11:59pm
strRestriction = "[Start] <= '" & Format$(dtRangeEnd, "mm/dd/yyyy hh:mm AMPM") & _
"' AND [End] >= '" & Format$(dtRangeStart, "mm/dd/yyyy hh:mm AMPM") & _
"' AND [BusyStatus] = " & CStr(olBusy)
Set oItemsForExport = oItems.Restrict(strRestriction)
' Open File for Export.
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(output_file)
' Debug.Print Format$(dtRangeStart, "mm/dd/yyyy hh:mm AMPM"), Format$(dtRangeEnd, "mm/dd/yyyy hh:mm AMPM")
For Each oItem In oItemsForExport
' Get appointment's start date/time.
dtItemStartTime = oItem.Start - Int(oItem.Start)
' Get appointment's end date/time.
dtItemEndTime = oItem.End - Int(oItem.End)
If InStr(1, oItem.Categories, "Busy", 1) > 0 Then
oFile.WriteLine Format$(oItem.Start, dtFormat) & vbTab & Format$(oItem.End, dtFormat)
' Debug.Print Format$(oItem.Start, dtFormat) & vbTab & Format$(oItem.End, dtFormat)
End If
Next oItem
' Close File
oFile.Close
Set fso = Nothing
Set oFile = Nothing
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """C:\ProgramData\Microsoft\Windows\Start Menu\Programs\CalendarUpdate.bat.lnk"""
MsgBox ("All Done")
End Sub