Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:29:58+00:00 2026-05-28T20:29:58+00:00

The code below happily populates an email with the week’s appoinments but it lists

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:29:59+00:00Added an answer on May 28, 2026 at 8:29 pm

    I have not reviewed your existing code although I did spot:

    1. If you created a list of my appointments and omitted my all day meetings, I would be seriously displeased.
    2. AddToReportIfNotBlank is not a function because it does not return a value.

    With my solution you do not add appointments to Report as 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.

    Type typAppointment
      Start As Date
      AllDay As Boolean
      End As Date
      Subject As String
      Location As String
    End Type
    

    I need these variables in addition to yours:

      Dim AppointmentDtl() As typAppointment
      Dim InxADCrnt As Long
      Dim InxADCrntMax As Long
      Dim InxAppointmentSorted() As Long
      Dim InxSrtCrnt1 As Long
      Dim InxSrtCrnt2 As Long
      Dim Stg as String
    

    This code prepares the array of structures for use. Place before the loop that looks for appointments:

      ReDim AppointmentDtl(1 To 100)
      ' * I avoid having too many ReDim Preserves because they
      '   involve creating a copy of the original array.
      ' * 100 appointments should be enough but the array will
      '   be resized if necessary.
      InxADCrntMax = 0      ' The current last used entry in AppointmentDtl
    

    Delete your code:

            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
    

    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:

            InxADCrntMax = InxADCrntMax + 1
            If InxADCrntMax > UBound(AppointmentDtl) Then
              ' Have filled array.  Add another 100 entries
              ReDim Preserve AppointmentDtl(1 To 100 + UBound(AppointmentDtl))
            End If
            AppointmentDtl(InxADCrntMax).Start = .Start
            If .AllDayEvent Then
              AppointmentDtl(InxADCrntMax).AllDay = True
            Else
              AppointmentDtl(InxADCrntMax).AllDay = False
              AppointmentDtl(InxADCrntMax).End = .End
            End If
            AppointmentDtl(InxADCrntMax).Subject = .Subject
            AppointmentDtl(InxADCrntMax).Location = .Location
          End If
    

    Above Call CreateReportAsEmail("List of Appointments", Report) insert:

      ' Initialise index array as 1, 2, 3, 4, ...
      ReDim InxAppointmentSorted(1 To InxADCrntMax)
      For InxSrtCrnt1 = 1 To InxADCrntMax
        InxAppointmentSorted(InxSrtCrnt1) = InxSrtCrnt1
      Next
    
      ' Sort index array by AppointmentDtl(xxx).Start.
      ' This is not an efficient sort but it should be sufficient for your purposes.
      ' If not, I have a Shell Sort written in VBA although a Quick Sort
      ' is considered the best.
      InxADCrnt = 1
      Do While InxADCrnt < InxADCrntMax
        InxSrtCrnt1 = InxAppointmentSorted(InxADCrnt)
        InxSrtCrnt2 = InxAppointmentSorted(InxADCrnt + 1)
        If AppointmentDtl(InxSrtCrnt1).Start > AppointmentDtl(InxSrtCrnt2).Start Then
          InxAppointmentSorted(InxADCrnt) = InxSrtCrnt2
          InxAppointmentSorted(InxADCrnt + 1) = InxSrtCrnt1
          If InxADCrnt > 1 Then
            InxADCrnt = InxADCrnt - 1
          Else
            InxADCrnt = InxADCrnt + 1
          End If
        Else
          InxADCrnt = InxADCrnt + 1
        End If
      Loop
    
      ' InxAppointmentSorted() is now: 5, 20, 2, ... where appointment 5 is
      ' the earliest, appointment 20 the next and so on
    
      ' Process appointments in Start order
      For InxSrtCrnt1 = 1 To InxADCrntMax
        InxADCrnt = InxAppointmentSorted(InxSrtCrnt1)
        With AppointmentDtl(InxADCrnt)
          ' I have tested all other code on my calendar.  This code is untested.
          ' I have included all day meetings but you could easily restore the
          ' original approach.
          Call AddToReportIfNotBlank(Report, "Subject", .Subject)
          If .AllDay Then
            Stg = "All day " & Format(.Start, "dddd d mmm")
          Else
            ' Date formatted as "Friday 27 Jan". Use "dddd mmmm, d" if you
            ' prefer "Friday January, 27".  That is: "d" gives day of month
            ' with leading zero omitted. "dddd" gives full day of week. "mmm"
            ' gives three letter month.  "mmmm" gives full month.  "yy", if
            ' required, give two day year. "yyyy" gives four day year. Include
            ' spaces and punctuation as desired. 
            Stg = Format(.Start, "dddd d mmm") & _
                  Format(.Start, " hh:mm") & " to " & _
                  Format(.End, "hh:mm")
          End If
          Call AddToReportIfNotBlank(Report, "When", Stg)
          Call AddToReportIfNotBlank(Report, "Location", .Location)
          Report = Report & "-----------------------------------------------------"
          Report = Report & vbCrLf & vbCrLf
        End With
      Next
    

    I hope I have included enough comments so all this makes sense. Do come back with questions is necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below works. But if I comment out the line Dim objRequest As
The code below works, but I suspect there is be a cleaner. Any alternative
My code below kinda works, it creates the User object and saves but it
Code below show date in format 02/15/2012. how to change it to 15/02/2012 Private
code below working fine but not in IE6, IE7, below is the code is
The code below should print ten results but instead it prints ten test-ite. Why
The code below works fine but, in the textbox the decimal value has this
Code below is used to create post data from jqGrid colmondel and post it.
The code below works beautifully in Tomcat, but the call to getResource(...) returns null
The code below gets the information i require from my database but is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.