-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain Macro (for viewing only as already integrated to spreadsheet).bas
More file actions
158 lines (117 loc) · 5.13 KB
/
Copy pathMain Macro (for viewing only as already integrated to spreadsheet).bas
File metadata and controls
158 lines (117 loc) · 5.13 KB
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
Attribute VB_Name = "Cal_Export"
Option Explicit
Public Sub Main()
'''''''''''''''''''''''''''''''''
' SETUP '
'''''''''''''''''''''''''''''''''
Debug.Print "Setting up Outlook..."
Dim olApp As Outlook.Application
Dim olAppt As Outlook.AppointmentItem
Dim Folders As Outlook.Folder
Dim subFolder, CalFolder As Outlook.MAPIFolder
Dim olNS As Outlook.Namespace
Dim blnCreated As Boolean
Dim arrCal As String
Dim i, j As Long
Dim bool As Boolean
Dim ImportBlanks As Boolean
Dim SkipBlanks As Boolean
Dim Response As Variant
'Initiate instance of Outlook
On Error Resume Next
Set olApp = GetObject("", "Outlook.Application")
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
MsgBox "Outlook is not available! Please open and retry again..." 'make dynamic
Exit Sub
End If
End If
'On Error GoTo Err_Execute
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set CalFolder = olNS.GetDefaultFolder(olFolderCalendar)
'''''''''''''''''''''''''''''''''
' Routine '
'''''''''''''''''''''''''''''''''
i = 2 'omit title row
SkipBlanks = False
'Loop rows until Cells(i, 1) is empty
Do Until Trim(Cells(i, 1).Value) = ""
'Error handling for blank cells.
If Cells(i, 8) = "" And ImportBlanks = False And SkipBlanks = False Then
Response = MsgBox("You have not selected whether you want to import this item..." _
& Chr(10) & "Import - " & Cells(i, 2).Value & "?", vbYesNo)
If Response = vbYes Then
Response = MsgBox("Do you want to import all future blank items?", vbYesNo)
If Response = vbYes Then ImportBlanks = True
GoTo ImportItem
Else
Response = MsgBox("Do you want to ignore all future blank items?", vbYesNo)
If Response = vbYes Then SkipBlanks = True
End If
'Import Entry
ElseIf Cells(i, 8).Value = True Or _
(ImportBlanks = True And IsEmpty(Cells(i, 8)) = True) Then
ImportItem:
Debug.Print "Importing - " & Cells(i, 2).Value
arrCal = Cells(i, 1).Value '****Accounts get their own calendar
'Conditional to add new Folders if Folder does not exist.
bool = True
For j = 1 To CalFolder.Folders.Count
If CalFolder.Folders.Item(j).Name = arrCal Then
bool = False
Exit For
End If
Next j
If bool = True Then
Set subFolder = CalFolder.Folders.Add(arrCal, olFolderCalendar)
Else
Set subFolder = CalFolder.Folders(arrCal)
End If
'Add New Appointment
Set olAppt = subFolder.Items.Add(olAppointmentItem)
'Define calendar item properties
'For more info visit:
'https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.appointmentitem_properties.aspx
With olAppt
.Start = Cells(i, 3)
.Subject = Cells(i, 2).Value 'Header
.Start = Cells(i, 3).Value
If IsEmpty(Cells(i, 4)) = True Then
.AllDayEvent = True 'End/ All day event
Else: .End = Cells(i, 4).Value
End If
If IsEmpty(Cells(i, 5)) = True Then
.ReminderSet = False 'Reminder
Else: .ReminderMinutesBeforeStart = Cells(i, 5).Value
End If
.Location = Cells(i, 6).Value
.Body = Cells(i, 7).Value
'to mark automated emails (highly recommended)
.Categories = "Orange Category"
.Save
End With
Cells(i, 8) = False 'Save current state
End If
i = i + 1
Loop
Set olAppt = Nothing
Set olApp = Nothing
ThisWorkbook.Save
MsgBox "Outlook has been updated!", , "Export to Outlook"
End Sub
'OPTIONAL:
'Use this function to quickly and effectively delete all appointment items within a specified folder
Private Sub ClearAppointments(CalFolder As Outlook.MAPIFolder)
Dim Appt As Object
Redirect:
For Each Appt In CalFolder.Items
If Appt.Class = olAppointment Then
'Debug.Print Appt.Subject & " - Deleted..."
Appt.Delete
End If
Next Appt
If Not CalFolder.Items.Count = 0 Then GoTo Redirect
End Sub