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
| Option Explicit
Public R As Long
Public SelectedCalendar As String
' ********************************************************************************
' * Interrogate Outlook calendars for all external meetings & populate
' * Excel database with attendees coming down the columns for each appt.
' **********************************************************************************
Sub outlook_calendaritemsexport()
Dim C As Long, i As Long, lrow As Long
Dim appt_id As Long, append_row As Long
Dim data_array() As Variant, start_time As Variant
Dim myfol As Outlook.Folder
Dim ons As Outlook.Namespace
Dim o As Outlook.Application
Dim myapt As Outlook.AppointmentItem
Dim myrpnt As Outlook.Recipient
Dim oEU As Object
Set o = New Outlook.Application
Set ons = o.GetNamespace("MAPI")
start_time = Now()
Sheets("Data").Activate
'Show UserForm here. UserForm is a simple box with names of shared calendars, which gets stored into "SelectedCalendar" upon selection. R is also set to 5 (start row on my sheet), or last row after current data
UserForm1.Show
'Setup 'Subroutine to Setup column widths / colors etc
append_row = R 'use this to know where to append the new data onto screen for an appended search.. R comes from UserForm.
Dim myRecipient As Outlook.Recipient
'selected in the userform ComboBox list
If SelectedCalendar = "Select Calendar" Then 'nobody selected, so operate on your own calendar
Set myfol = ons.GetDefaultFolder(olFolderCalendar) 'Set this to work on own folder
Else
Set myRecipient = ons.CreateRecipient(SelectedCalendar)
myRecipient.Resolve
If myRecipient.Resolved Then
Set myfol = ons.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
Else
MsgBox ("Calendar Issue, Program Halted")
End 'end if calendar not resolved
End If
End If
Range("A4:N4").Value = Array("DATE", "CUSTOMER", "SUBJECT", "LOCATION", "CUSTOMER TYPE or DISTRIBUTOR MEETING", "FACE To FACE / TEAMS", "DISTRIBUTOR Or ALONE", "DISTRIBUTOR VISIT TYPE", "CUSTOMER ATTENDEES", "DISTRIBUTOR ATTENDEES", "OUR ATTENDEES", "REQUIRED ATTENDEES", "CALENDAR OWNER", "MEET ID")
'check calendar has some items inside / is shared
On Error GoTo ErrorHandler
lrow = 0 'array row start)
ReDim Preserve data_array(1 To 14, lrow) '(be aware the array is transposed, as ReDim Preserve only works on last dimension)
'*** GET THIS DYNAMICALLY FROM LAST ROW OF DATA (comes from userform button) in case of New, or Appended data search *****
If R = 5 Then
appt_id = 1 'set first appointment ID number
Else
appt_id = Cells(R - 1, 14).Value + 1
End If
R = 0 ' now reset R as first row in array
For Each myapt In myfol.Items 'check each calendar Appointment) Then
'Year(Now()) Year(Now())-1 this year last year
If InStr(myapt.Start, Year(Now())) > 0 Or InStr(myapt.Start, (Year(Now())) - 1) > 0 Then ' if appt is this year/last year
data_array(1, R) = myapt.Start
data_array(3, R) = myapt.Subject
data_array(4, R) = myapt.Location
data_array(12, R) = LCase(myapt.RequiredAttendees)
data_array(13, R) = SelectedCalendar
data_array(14, R) = appt_id
appt_id = appt_id + 1 ' update appointment ID code
ReDim Preserve data_array(1 To 14, R + 1)
R = R + 1
End If 'End Appt year checking
Next 'Calendar Appointment
Set o = Nothing
Set ons = Nothing
Set myfol = Nothing
Set myapt = Nothing
Set myrpnt = Nothing
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
'store data on screen
Range(Cells(append_row, 1), Cells(append_row + UBound(data_array, 2), 14)) = WorksheetFunction.Transpose(data_array) 'function to transpose data back into rows due to RedimPreserve operating across only
MsgBox ("start : " & start_time & " finish : " & Now())
Erase data_array
End
ErrorHandler:
MsgBox ("Calendar Not Shared")
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End
End Sub |
Partager