Dear All,
I'm trying to use Excel VBA to create appointments in Office 365 on 13th of month or next working day.
I enter Start Date 01/01/2023 and End Date 31/12/2023 it creates appointments on Weekends too. How do I fix this code?
Your help would be greatly appreciated.
Kind Regards
Biz
I'm trying to use Excel VBA to create appointments in Office 365 on 13th of month or next working day.
VBA Code:
Sub SetupRecurringAppointmentFix501()
' Get start date and end date from user input
Dim startDate As Date
Dim endDate As Date
startDate = InputBox("Enter start date (dd/mm/yyyy):")
endDate = InputBox("Enter end date (dd/mm/yyyy):")
' Set up Outlook objects
Dim olApp As Outlook.Application
Dim olApt As Outlook.AppointmentItem
Set olApp = CreateObject("Outlook.Application")
Set olApt = olApp.CreateItem(olAppointmentItem)
' Set up recurring appointment properties
With olApt
.Subject = "Monthly Meeting"
.Location = "Conference Room"
' Find the next available weekday after the 13th day of the month
Dim nextWeekday As Date
nextWeekday = DateSerial(Year(startDate), Month(startDate), 13)
Do Until Weekday(nextWeekday) >= 2 And Weekday(nextWeekday) <= 6
nextWeekday = nextWeekday + 1
Loop
.Start = nextWeekday + TimeSerial(10, 0, 0)
.End = .Start + TimeSerial(4, 0, 0) ' Appointment duration of 4 hour
.ReminderMinutesBeforeStart = 15 ' Reminder set for 15 minutes before start time
.ReminderSet = True ' Reminder is enabled
.BusyStatus = olBusy ' Set appointment status as busy
.Save ' Save appointment
.Display
End With
' Set up recurrence pattern
Dim RecurrPat As Outlook.RecurrencePattern
Set RecurrPat = olApt.GetRecurrencePattern
With RecurrPat
.RecurrenceType = olRecursMonthly ' Recurs monthly
.Interval = 1 ' Occurs every 1 month
.DayOfWeekMask = olMonday + olTuesday + olWednesday + olThursday + olFriday 'Occurs on weekdays only
.PatternStartDate = startDate ' Starts on user-specified start date
.PatternEndDate = endDate ' Ends on user-specified end date
End With
' Release Outlook objects
Set olApt = Nothing
Set olApp = Nothing
End Sub
I enter Start Date 01/01/2023 and End Date 31/12/2023 it creates appointments on Weekends too. How do I fix this code?
Your help would be greatly appreciated.
Kind Regards
Biz