Hello,
I'm using my excel sheet to find an appointment within the next 2 weeks and send an email (I eventually want to find the next 2 appointments that aren't on the same day however small steps first!).
I've written the following code but cannot see where I'm going wrong as it cannot find any appointments within the next 2 weeks. Is anyone able to help?
Many thanks
I'm using my excel sheet to find an appointment within the next 2 weeks and send an email (I eventually want to find the next 2 appointments that aren't on the same day however small steps first!).
I've written the following code but cannot see where I'm going wrong as it cannot find any appointments within the next 2 weeks. Is anyone able to help?
Many thanks
VBA Code:
Option Explicit
Sub SendAvailableTimeSlots()
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFolder As Outlook.Folder
Dim olItems As Outlook.Items
Dim olRestrictItems As Outlook.Items
Dim olItem As Outlook.AppointmentItem
Dim olEmail As Outlook.MailItem
Dim olStartTime As Date
Dim olEndTime As Date
Dim olDuration As String
Dim olMaxDuration As String
Dim olFilter As String
Dim i As Long
Dim j As Long
Dim strBody As String
Dim olMaxDurationStr As String
' Define working hours and maximum duration
olStartTime = TimeValue("9:00:00 AM")
olEndTime = TimeValue("5:00:00 PM")
olDuration = "01:00:00"
olMaxDuration = TimeValue(olDuration) ' Convert olDuration to a number
olMaxDurationStr = Format(olMaxDuration, "hh:mm:ss") ' Convert olMaxDuration to a string
' olMaxDuration = olDuration
' Get the Outlook calendar folder
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderCalendar)
Set olItems = olFolder.Items
' Apply filters to find available time slots
Dim n As Long
Debug.Print TimeValue(olDuration) 'this gives an answer of 01:00:00
'Find the next available free time slot
olFilter = "[Start] >= '" & Format(Now(), "dd/mm/yyyy hh:mm:ss") & "'"
olFilter = olFilter & " And [End] <= '" & Format(DateAdd("d", 14, Now()), "dd/mm/yyyy hh:mm:ss") & "'"
olFilter = olFilter & " And [BusyStatus] = 0"
olFilter = olFilter & " And [Start] >= '" & Format(olStartTime, "hh:mm:ss") & "'"
olFilter = olFilter & " And [End] <= '" & Format(olEndTime, "hh:mm:ss") & "'"
'If TimeValue(olDuration) > TimeValue("00:00:00") Then
' olFilter = olFilter & " And DateDiff('n', [Start], [End]) >= " & DateDiff("n", 0, TimeValue(olDuration))
'End If
'I'm not sure whether to include the above 3 lines - if I do then I get 'condition is not valid' at the next line. If I don't then the code can't find any appointments anyway (olRestrictItems.count = 0)
Set olRestrictItems = olItems.Restrict(olFilter)
' Sort the appointment items by start time
olRestrictItems.Sort "[Start]"
' Create a new email object
Set olEmail = olApp.CreateItem(olMailItem)
With olEmail
.Subject = "Available time slots"
.Body = "Dear client," & vbCrLf & vbCrLf & "I am available at the following times within the next 2 weeks:" & vbCrLf & vbCrLf
' Insert the next 2 available time slots in the email body
i = 1
j = 1
Do While i <= olRestrictItems.Count And j <= 1
Set olItem = olRestrictItems.Item(i)
If olItem.Start > Now() Then ' Only include appointments in the future
strBody = Format(olItem.Start, "dd/mm/yyyy hh:mm:ss") & " to " & Format(DateAdd("h", olMaxDuration, olItem.Start), "hh:mm") & vbCrLf
.Body = .Body & strBody
j = j + 1
End If
i = i + 1
Loop
If j = 1 Then ' No available time slots found
.Body = .Body & "Sorry, no available time slots were found within the next 2 weeks that match your requirements." & vbCrLf
End If
.Display ' Display the email
End With
' Clean up
Set olItem = Nothing
Set olEmail = Nothing
Set olRestrictItems = Nothing
Set olItems = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub