The code below happily populates an email with the week’s appoinments but it lists the calendar items in the email by appointment create date rather than the actual appointment date. Is there a way to list the items by appointment date?
My humble thanks for any help or suggestions.
(I cannot take credit for this code as I pasted together pieces found on the net. I am more familiar with Excel and Access VBA than with Outlook. Again my thanks.) John
Public Sub ListAppointments()
On Error GoTo On_Error
Dim Session As Outlook.NameSpace
Dim Report As String
Dim AppointmentsFolder As Outlook.Folder
Dim currentItem As Object
Dim currentAppointment As AppointmentItem
Set Session = Application.Session
Set AppointmentsFolder = Session.GetDefaultFolder(olFolderCalendar)
For Each currentItem In AppointmentsFolder.Items
If (currentItem.Class = olAppointment) Then
Set currentAppointment = currentItem
'get the week's appointments
If currentAppointment.Start >= Now() And currentAppointment.Start <= Now() + 7 Then
If currentAppointment.AllDayEvent = False Then 'exclude all day events
Call AddToReportIfNotBlank(Report, "Subject", currentAppointment.Subject)
Call AddToReportIfNotBlank(Report, "Start", currentAppointment.Start)
Call AddToReportIfNotBlank(Report, "End", currentAppointment.End)
Call AddToReportIfNotBlank(Report, "Location", currentAppointment.Location)
Report = Report & "-----------------------------------------------------"
Report = Report & vbCrLf & vbCrLf
End If
End If
End If
Next
Call CreateReportAsEmail("List of Appointments", Report)
Exiting:
Exit Sub
On_Error:
MsgBox "error=" & Err.Number & " " & Err.Description
Resume Exiting
End Sub
Private Function AddToReportIfNotBlank(Report As String, FieldName As String, FieldValue)
AddToReportIfNotBlank = ""
If (IsNull(FieldValue) Or FieldValue <> "") Then
AddToReportIfNotBlank = FieldName & " : " & FieldValue & vbCrLf
Report = Report & AddToReportIfNotBlank
End If
End Function
'publish items to Outlook email
Public Sub CreateReportAsEmail(Title As String, Report As String)
On Error GoTo On_Error
Dim objNS As Outlook.NameSpace
Dim objItem As MailItem
Dim objFolder As MAPIFolder
Set objNS = Application.GetNamespace("MAPI") 'Application.Session
Set objItem = Application.CreateItem(olMailItem)
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
With objItem
.Subject = "This weeks appointments"
.Body = Report
.Display
End With
Exiting:
'Set Session = Nothing
Exit Sub
On_Error:
'MsgBox "error=" & Err.Number & " " & Err.Description
Resume Exiting
End Sub
I have not reviewed your existing code although I did spot:
AddToReportIfNotBlankis not a function because it does not return a value.With my solution you do not add appointments to
Reportas they are discovered. Instead they are added to an array of structures. Once all relevant appointments have been found, an array of indices into the array of structures is created and sorted by appointment date. Report is then built from the array of structures in index sequence. I hope that makes sense. Extra detail in the code.My solution requires a structure. The type definition must be placed before any subroutines or functions.
I need these variables in addition to yours:
This code prepares the array of structures for use. Place before the loop that looks for appointments:
Delete your code:
and replace with the following which stores the detail of the selected appointments in the structure. This code handles all day meetings as well as part day meetings:
Above
Call CreateReportAsEmail("List of Appointments", Report)insert:I hope I have included enough comments so all this makes sense. Do come back with questions is necessary.