Question
Sub ScheduleExams() 'Declare variables Dim examDate As Date Dim examTime As Date Dim examDuration As Integer Dim examLocation As String Dim calendarFolder As String Dim
Sub ScheduleExams()
'Declare variables
Dim examDate As Date
Dim examTime As Date
Dim examDuration As Integer
Dim examLocation As String
Dim calendarFolder As String
Dim emailSubject As String
Dim emailBody As String
Dim emailRecipient As String
'Array to store exam details
Dim exams(1 To 3) As Variant
'Loop through exams
For i = 1 To 3
'Prompt user for exam date and time
exams(i, 1) = InputBox("Enter the exam date (mm/dd/yyyy) for exam #" & i & ":")
exams(i, 2) = InputBox("Enter the exam time (hh:mm AM/PM) for exam #" & i & ":")
'Validate exam date and time
If Not IsDate(exams(i, 1)) Then
MsgBox "Error: Invalid exam date. Please enter a valid exam date (mm/dd/yyyy)."
Exit Sub
End If
If Not TimeValue(exams(i, 2)) Then
MsgBox "Error: Invalid exam time. Please enter a valid exam time (hh:mm AM/PM)."
Exit Sub
End If
'Combine exam date and time into a single Date variable
exams(i, 1) = DateValue(exams(i, 1)) + TimeValue(exams(i, 2))
'Check if exam time is valid (not in the past)
If exams(i, 1) < Now Then
MsgBox "Error: Exam time is in the past. Please enter a valid exam time."
Exit Sub
End If
'Prompt user for exam duration
exams(i, 3) = InputBox("Enter the exam duration (in minutes) for exam #" & i & ":")
'Validate exam duration
If Not IsNumeric(exams(i, 3)) Then
MsgBox "Error: Invalid exam duration. Please enter a valid exam duration (in minutes)."
Exit Sub
End If
'Prompt user for exam location
exams(i, 4) = InputBox("Enter the exam location for exam #" & i & ":")
Next i
'Prompt user for calendar folder
calendarFolder = InputBox("Enter the name of the calendar folder where the exams will be created:")
'Prompt user for email subject and body
emailSubject = InputBox("Enter the subject for the email notification:")
emailBody = InputBox("Enter the body for the email notification:")
'Prompt user for email recipient
emailRecipient = InputBox("Enter the email address of the recipient:")
'Create a new Outlook application
Dim olApp As Object
Set olApp = CreateObject("Outlook.Application")
'Get the calendar folder
Dim olNS As Object
Set olNS = olApp.GetNamespace("MAPI")
Dim olCalendar As Object
Set olCalendar = olNS.GetDefaultFolder(9) '9 = olFolderCalendar
Dim targetCalendar As Object
Set targetCalendar = olCalendar.Folders(calendarFolder)
'Loop through exams
For i = 1 To 3
'Create a new appointment in Outlook
Dim olAppt As Object
Set olAppt = olApp.CreateItem(1) '1 = olAppointmentItem
With olAppt
.Start = exams(i, 1)
.Duration = exams(i, 3)
.Location = exams(i, 4)
.Subject = "Exam #" & i
.Save
End With
'Send email notification
Dim olMail As Object
Set olMail = olApp.CreateItem(0) '0 = olMailItem
With olMail
.To = emailRecipient
.Subject = emailSubject
.Body = emailBody
.Attachments.Add olAppt, 1, , "Exam #" & i & ".ics" '1 = olEmbeddeditem
.Send
End With
Next i
'Export exams to CSV file
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim file As Object
Set file = fso.CreateTextFile("exams.csv", True)
'Write header row
file.WriteLine "Date,Time,Duration,Location"
'Loop through exams and write to file
For i = 1 To 3
file.WriteLine exams(i, 1) & "," & exams(i, 2) & "," & exams(i, 3) & "," & exams(i, 4)
Next i
file.Close
'Confirm exam scheduling
MsgBox "Exams successfully scheduled and exported to exams.csv"
End Sub
Find the error in the Excel Code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started