Getting reccuring appointments VBA - excel

Can somebody please help me out. I have this code and it reads all appointments from the calendar for specific dates; however, the code doesn't display any recurring meetings within given dates:
ToDate = CDate("10/12/2019")
FromDate = CDate("10/06/2019")
'On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar
'include reccuring items
'-------------------------
olFolder.Items.Sort ("[Start]")
olFolder.Items.IncludeRecurrences = True
'-------------------------
NextRow = 2
With Sheets("Sheet1") 'Change the name of the sheet here
.Range("A1:F1").Value = Array("Report Date", "Date", "Time spent", "Location", "Categories", "Title")
For Each olApt In olFolder.Items
If (olApt.Start >= FromDate And olApt.Start <= ToDate) Then
.Cells(NextRow, "A").Value = Format(Now, "DD-MM-YY")
.Cells(NextRow, "B").Value = CDate(olApt.Start)
.Cells(NextRow, "C").Value = olApt.End - olApt.Start
.Cells(NextRow, "C").NumberFormat = "HH:MM"
.Cells(NextRow, "E").Value = olApt.Categories
.Cells(NextRow, "F").Value = olApt.Subject
NextRow = NextRow + 1
Else
End If
Next olApt
.Columns.AutoFit
End With
Can somebody please help me out and let me know what I am doing wrong?!
Thank you!

Items in a folder have to be made into a collection to then manipulate.
On Error Resume Next ' This is a rare proper use.
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar
Set itmCollection = olFolder.Items
'include recurring items
'-------------------------
itmCollection.Sort ("[Start]")
itmCollection.IncludeRecurrences = True
'-------------------------
For Each olApt In itmCollection
Demo code
Option Explicit
Sub apptsInDateRangeIncludingRecurrences()
Dim ToDate As Date
Dim FromDate As Date
Dim olApp As Object
Dim olNS As Object
Dim olFolder As Object
Dim itmCollection As Object
Dim itmCollectionFrom As Object
Dim itmCollectionFromTo As Object
Dim sFilter As String
Dim olApt As Object
ToDate = CDate("10/12/2019")
FromDate = CDate("10/06/2019")
On Error Resume Next ' This is a rare proper use.
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar
Set itmCollection = olFolder.Items
'include recurring items
'-------------------------
itmCollection.Sort ("[Start]")
itmCollection.IncludeRecurrences = True
'-------------------------
'Filter for applicable items
sFilter = "[Start]>='" & FromDate & "'"
'Debug.Print sFilter
Set itmCollectionFrom = itmCollection.Restrict(sFilter)
sFilter = "[Start]<='" & ToDate & "'"
'Debug.Print sFilter
Set itmCollectionFromTo = itmCollectionFrom.Restrict(sFilter)
For Each olApt In itmCollectionFromTo
If olApt.Start >= FromDate Then
Debug.Print olApt.Start & " " & olApt.Subject
End If
Next olApt
Debug.Print "Done."
End Sub

Related

VBA Shared Calendar Meeting Creator

I am using a modified code I found online, and I am having issues finding a shared calendar in outlook.
Sub SharedCalendarEventCreator()
Dim olApp As Outlook.Application
Dim outNameSpace As Namespace
Dim outSharedName As Outlook.Recipient
Dim outCalendarFolder As MAPIFolder
Dim olAppItem As Outlook.AppointmentItem
Dim r As Long
Dim eduSheet As Worksheet
On Error Resume Next
Worksheets("Schedule").Activate
Set olApp = GetObject("", "Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
On Error Resume Next
Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
MsgBox "Outlook is not available!"
Exit Sub
End If
End If
Set outNameSpace = olApp.GetNamespace("MAPI")
Set outCalendarFolder = outNameSpace.Folders("Lunch Calendar")
Set outCalendarFolder = outCalendarFolder.Folders("Calendar")
Set olAppItem = outCalendarFolder.Items.Add(olAppointmentItem)
r = 7
Do Until Trim$(Cells(r, 1).Value) = ""
With olAppItem
.Subject = "SOF " & Cells(1, 2).Value & " " & Cells(2, 2).Value & " " & Cells(3, 2).Value & " " & Cells(r, 2).Value
.Start = Cells(r, 1).Value
vArray = Split(Cells(4, 2).Value2, ";")
For Each vElement In vArray
'.Recipients.Add .Recipients.Add(vElement)
Next vElement
.MeetingStatus = olMeeting
.AllDayEvent = True
.Body = Cells(r, 3).Value
.ResponseRequested = False
.Send
r = r + 1
End With
Loop
Set olAppItem = Nothing
Set outCalendarFolder = Nothing
Set outNameSpace = Nothing
Set olApp = Nothing
End Sub
Picture of the Calendar location:
I believe the issue is within these lines, but I have tried to change them without any luck:
Set outNameSpace = olApp.GetNamespace("MAPI")
Set outCalendarFolder = outNameSpace.Folders("Lunch Calendar")
Set outCalendarFolder = outCalendarFolder.Folders("Calendar")
Set olAppItem = outCalendarFolder.Items.Add(olAppointmentItem)
The error I received:
When I hit debug, this is the line it highlights:
Any help is greatly appreciated.
Navigate the folder tree from "janedoe#gmail" to "Calendar" to "Lunch Calendar".
Option Explicit
Sub SharedCalendarEventCreator()
' Early binding - Set reference to Outlook XX.X Object Library
Dim olApp As Outlook.Application
Dim outNameSpace As Outlook.Namespace
Dim outMailboxFolder As Outlook.Folder
Dim outCalendarFolder As Outlook.Folder
Dim outCalendarSubFolder As Outlook.Folder
Dim olAppItem As Outlook.AppointmentItem
On Error Resume Next
Set olApp = GetObject("", "Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
On Error Resume Next
Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
MsgBox "Outlook is not available!"
Exit Sub
End If
End If
Set outNameSpace = olApp.GetNamespace("MAPI")
Set outMailboxFolder = outNameSpace.Folders("janedoe#gmail")
Set outCalendarFolder = outMailboxFolder.Folders("Calendar")
Set outCalendarSubFolder = outCalendarFolder.Folders("Lunch Calendar")
'Set ActiveExplorer.CurrentFolder = outCalendarSubFolder
Set olAppItem = outCalendarSubFolder.Items.Add(olAppointmentItem)
olAppItem.Display
Set olAppItem = Nothing
Set outCalendarSubFolder = Nothing
Set outCalendarFolder = Nothing
Set outMailboxFolder = Nothing
Set outNameSpace = Nothing
Set olApp = Nothing
End Sub

Accessing Custom Outlook Calendar Entries

I'm trying to access calendar entries from 2 custom Outlook calendars using Excel VBA.
I've obtained some code which gives me what I want from the default calendar but I cannot see how to change the location to my own calendars.
The code I'm using is
Sub ListAppointments()
Dim olApp As Object
Dim olNS As Object
Dim olFolder As Object
Dim olApt As Object
Dim nextrow As Long
Dim FromDate As Date
Dim ToDate As Date
FromDate = CDate("30/11/2021")
ToDate = CDate("20/12/2021")
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9)
nextrow = 2
With Sheets("Cal-Ext")
.Range("A1:E1").Value = Array("Date", "Start Time", "End Time", "Subject", "Location")
For Each olApt In olFolder.Items
If (olApt.Start >= FromDate And olApt.Start <= ToDate) Then
.Cells(nextrow, "A").Value = CDate(olApt.Start)
.Cells(nextrow, "A").NumberFormat = "DD/MM/YYYY"
.Cells(nextrow, "B").Value = olApt.Start
.Cells(nextrow, "B").NumberFormat = "HH:MM"
.Cells(nextrow, "C").Value = olApt.End
.Cells(nextrow, "C").NumberFormat = "HH:MM"
.Cells(nextrow, "D").Value = olApt.Subject
.Cells(nextrow, "E").Value = olApt.Location
nextrow = nextrow + 1
Else
End If
Next olApt
Set olFolder = olNS.GetDefaultFolder(9)
nextrow = nextrow + 5
For Each olApt In olFolder.Items
If (olApt.Start >= FromDate And olApt.Start <= ToDate) Then
.Cells(nextrow, "A").Value = CDate(olApt.Start)
.Cells(nextrow, "A").NumberFormat = "DD/MM/YYYY"
.Cells(nextrow, "B").Value = olApt.Start
.Cells(nextrow, "B").NumberFormat = "HH:MM"
.Cells(nextrow, "C").Value = olApt.End
.Cells(nextrow, "C").NumberFormat = "HH:MM"
.Cells(nextrow, "D").Value = olApt.Subject
.Cells(nextrow, "E").Value = olApt.Location
nextrow = nextrow + 1
Else
End If
Next olApt
.Columns.AutoFit
End With
Set olApt = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub
I need to change the set olfolder command before the second loop to select my own calendar but nothing I've tried works.
Outlook calendar
Current excel result
Here is a quick code that I wrote to loop through all the navigation folders of My Calendars
This code was tested in MS Outlook. You may have to edit it for it to work in MS Excel.
Option Explicit
Sub Sample()
Dim oNameSpace As Object
Dim oExplorer As Object
Dim oMainFolder As Object
Dim oCalModule As Object
Dim oSubFolder As Object
Dim oCalNavFolders As Object
Dim i As Long
Dim objitem As Object
Set oNameSpace = Outlook.GetNamespace("MAPI")
Set oExplorer = oNameSpace.GetDefaultFolder(9).GetExplorer
Set oCalModule = oExplorer.NavigationPane.Modules.GetNavigationModule(1)
Set oCalNavFolders = oCalModule.NavigationGroups.Item("My Calendars").NavigationFolders
For i = 1 To oCalNavFolders.Count
Set objitem = oCalNavFolders(i)
On Error Resume Next
Set oSubFolder = objitem.Folder
On Error GoTo 0
If Not oSubFolder Is Nothing Then
Debug.Print oSubFolder.Name
If oSubFolder.Name = "Area1" Then
With oSubFolder
'
'~~> Do what you want
'
End With
Exit For
End If
Set oSubFolder = Nothing
End If
Next i
End Sub
Screenshot
Code from Excel
Option Explicit
Sub ListAppointments()
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
Dim oNameSpace As Object
Dim oExplorer As Object
Dim oMainFolder As Object
Dim oCalModule As Object
Dim oSubFolder As Object
Dim oCalNavFolders As Object
Dim i As Long
Dim objitem As Object
Set oNameSpace = OutApp.GetNamespace("MAPI")
Set oExplorer = oNameSpace.GetDefaultFolder(9).GetExplorer
Set oCalModule = oExplorer.NavigationPane.Modules.GetNavigationModule(1)
Set oCalNavFolders = oCalModule.NavigationGroups.Item("My Calendars").NavigationFolders
For i = 1 To oCalNavFolders.Count
Set objitem = oCalNavFolders(i)
On Error Resume Next
Set oSubFolder = objitem.Folder
On Error GoTo 0
If Not oSubFolder Is Nothing Then
Debug.Print oSubFolder.Name
If oSubFolder.Name = "Area1" Then
With oSubFolder
'
'~~> Do what you want
'
End With
Exit For
End If
Set oSubFolder = Nothing
End If
Next i
End Sub

Runtime error 462 when sending multiple email notifications

I have a worksheet set up to send email notifications when certain cells contain values, such as "75%" and "Due".
Around half the time I get
Runtime error 462
It seems this is because I have not specified elements of my code properly.
Private Sub Worksheet_Change(ByVal Target As Range)
Call Check_Project_Progress
End Sub
Private Sub Send_Email(Optional ByVal email_title As String = "")
Dim olNS As Namespace
Dim olMail As MailItem
Set olNS = GetNamespace("MAPI")
Set olMail = CreateItem(olMailItem)
With olMail
.Subject = email_title
.To = "LearnDataAnalysis#outlook.com"
.Body = "Value reads 75%"
.SendUsingAccount = olNS.Accounts.Item(1)
.Display
'.Send
End With
Set olMail = Nothing
Set olNS = Nothing
End Sub
Private Sub Send_Email2(Optional ByVal email_title As String = "")
Dim olNS As Namespace
Dim olMail As MailItem
Set olNS = GetNamespace("MAPI")
Set olMail = CreateItem(olMailItem)
With olMail
.Subject = email_title
.To = "LearnDataAnalysis#outlook.com"
.Body = "PLA determination due"
.SendUsingAccount = olNS.Accounts.Item(1)
.Display
'.Send
End With
Set olMail = Nothing
Set olNS = Nothing
End Sub
Private Sub Check_Project_Progress()
Dim LastRow As Long, RowNumber As Long
Dim ws As Worksheet
Set ws = ActiveSheet
With ws
LastRow = 500
If 4 > LastRow Then Exit Sub
For RowNumber = 4 To LastRow
If .Cells(RowNumber, "AB").Value = 0.75 And .Cells(RowNumber, "AD").Value <> "S" Then
.Cells(RowNumber, "AD").Value = "S"
.Cells(RowNumber, "AE") = "Email sent on:" & Now()
Call Send_Email(.Cells(RowNumber, "C").Value & " is approaching deadline")
End If
If 4 > LastRow Then Exit Sub
If .Cells(RowNumber, "AC").Value = "Due" And .Cells(RowNumber, "AD").Value <> "S,S" Then
.Cells(RowNumber, "AD").Value = "S,S"
.Cells(RowNumber, "AF") = "Email sent on:" & Now()
Call Send_Email2(.Cells(RowNumber, "C").Value & " has met deadline")
End If
Next RowNumber
End With
Set ws = Nothing
End Sub
Again, the error message is Runtime error 462. I have little idea what I'm doing.
You are getting that error becuase the code is not able to connect to the outlook application.
Try this. I have shown you for Send_Email. Adapt it for Send_Email2 as well.
Private Sub Send_Email(Optional ByVal email_title As String = "")
Dim olNS As Object
Dim olMail As Object
Dim OutlookApp As Object
'~~> Establish an Outlook application object
On Error Resume Next
Set OutlookApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set OutlookApp = CreateObject("Outlook.Application")
End If
Err.Clear
On Error GoTo 0
'~~> Check if we managed to connect to Outlook
If OutlookApp Is Nothing Then
MsgBox "unable to connect to outlook"
Exit Sub
End If
Set olNS = OutlookApp.GetNamespace("MAPI")
Set olMail = OutlookApp.CreateItem(0)
With olMail
.Subject = email_title
.To = "LearnDataAnalysis#outlook.com"
.Body = "Value reads 75%"
.SendUsingAccount = olNS.Accounts.Item(1)
.Display
'.Send
End With
End Sub

Looping over recipients in a Outlook Appointments

I using this code and trying to access the Recipients (Email Address and also Display Name) of each Outlook Appointment but getting a Error:
Run-time error '287' Application-defined or object-defined error
This error is highlighted on the line : For Each recip In olApt.recipients
Sub ListAppointments()
Dim olApp As Object
Dim olNS As Object
Dim olFolder As Object
Dim olApt As Object
Dim NextRow As Long
Dim FromDate As Date
Dim ToDate As Date
FromDate = CDate("01/04/2019")
ToDate = CDate("14/04/2019")
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar
NextRow = 2
With Sheets("Sheet1") 'Change the name of the sheet here
.Range("A1:D1").Value = Array("Meeting", "Date", "Location", "Invitees")
For Each olApt In olFolder.Items
If (olApt.Start >= FromDate And olApt.Start <= ToDate) Then
.Cells(NextRow, "A").Value = olApt.Subject
.Cells(NextRow, "B").Value = CDate(olApt.Start)
.Cells(NextRow, "C").Value = olApt.Location
.Cells(NextRow, "D").Value = olApt.Categories
Dim recip As Object
Dim allRecip As String
For Each recip In olApt
Debug.Print (recip.Address)
.Cells(NextRow, "E").Value = olApt.Address
Next
NextRow = NextRow + 1
Else
End If
Next olApt
.Columns.AutoFit
End With
Set olApt = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub
UPDATE
I tried
For Each recip in olApt.recipients
.Cells(NextRow, "E").Value = olApt.recipients.Address
Next
And I still get the errors below.
This is the error
Here are two parts of the Watch on olApt
UPDATE 2
The answer works on my laptop but crashes on my desktop (seperate outlook account). This is the line it crashes on, and I not it wont let the "R" in recipients be a captial (it changes to lower case automatically despite typing a captial).
I also note that the Recipients collection on olApt is different on my laptop to my desktop:
The line
Cells(NextRow, "E").Value = olApt.recipients.Address
must be replaced with
.Cells(NextRow, "E").Value = recip.Address
Also keep in mind that Outlook Security can blocks access to properties such as SenderEmailAddress or Recipients if an antivirus app is not installed or is out of date. See https://learn.microsoft.com/en-us/office/vba/outlook/how-to/security/security-behavior-of-the-outlook-object-model

Use Excel to make appointment in All Public Folders Calendar in 2013 MS Exchange

My code works fine in the default calendar but I am unable to get it to made an appointment in the AllPublicFolders Calendar. I am unable to call the function GetPublicFolder because I am new using VBA. Any help would be greatly appreciated.
Here is my code with "Big Store A Calendar in the all public folders:
Option Explicit
Sub RegisterAppointmentList()
' adds a list of appointments to the Calendar in Outlook
Dim olApp As Outlook.Application
Dim olAppItem As Outlook.AppointmentItem
Dim r As Long
On Error Resume Next
'Worksheets("Schedule").Activate
Worksheets("Appt").Activate
Set olApp = GetObject("", "Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
On Error Resume Next
Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
MsgBox "Outlook is not available!"
Exit Sub
End If
End If
'r = 6 ' first row with appointment data in the active worksheet
r = 2 ' first row with appointment data in the active worksheet
Dim mysub, myStart, myEnd
While Len(Cells(r, 2).Text) <> 0
mysub = Cells(r, 2) & ", " & Cells(r, 3)
myStart = DateValue(Cells(r, 5).Value) + Cells(r, 6).Value
myEnd = DateValue(Cells(r, 7).Value) + Cells(r, 8).Value
'DeleteTestAppointments mysub, myStart, myEnd
Set olAppItem = olApp.CreateItem(olAppointmentItem) ' creates a new appointment
With olAppItem
' set default appointment values
.Location = Cells(r, 2)
.Body = Cells(r, 3).Value
.ReminderSet = False
.BusyStatus = olFree
'.RequiredAttendees = "johndoe#microsoft.com"
On Error Resume Next
.Start = myStart
.End = myEnd
.Subject = Cells(r, 1)
.Location = Cells(r, 2)
.Body = Cells(r, 3).Value
'.ReminderSet = True
'.BusyStatus = olBusy
.Categories = Cells(r, 4).Value
On Error GoTo 0
.Save ' saves the new appointment to the default folder
End With
r = r + 1
Wend
Set olAppItem = Nothing
Set olApp = Nothing
' Print the Appt Sheet
Sheets("Sheet1").PrintOut
MsgBox "The Appt Sheet Printed and the Appt was entered in your default calendar on May 31st!"
End Sub
‘-------------------------I Need to get correct Public folder for the Exchange calendar -------------
‘I am using VBA for excel workbooks and need to create appointments in 2 public folder shared calendars
‘I need to get code like the code below to create appointments in the shared public calendar – ‘
‘I determine which calendar for the appointment using a workbook cell which is a list box of the 2 calendar names –
‘ Big Store A Calendar or Big Store B Calendar
' GetFolder - Gets a Public folder based on a string path - e.g.
'If Folder name in English is
'Public Folders\All Public Folders\Big Store A Calendar or
‘'Public Folders\All Public Folders\Big Store B Calendar
Public Function GetPublicFolder(strFolderPath)
Dim colFolders
Dim objFolder
Dim arrFolders
Dim i
On Error Resume Next
strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders = Split(strFolderPath, "\")
Set objFolder = Application.Session.GetDefaultFolder(18) ‘This is the correct folder # for “All Public Folders”
Set objFolder = objFolder.Folders.Item(arrFolders(0))
If Not objFolder Is Nothing Then
For i = 1 To UBound(arrFolders)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arrFolders(i))
If objFolder Is Nothing Then
Exit For
End If
Next
End If
Set GetPublicFolder = objFolder
Set colFolders = Nothing
' Set objApp = Nothing
Set objFolder = Nothing
End Function
Application in Set objFolder = Application.Session.GetDefaultFolder(18) is Excel. You want to use Outlook.
Sub DisplyOutlookPublicFolderFromExcel()
Dim olApp As Outlook.Application
Dim olAppItem As Outlook.AppointmentItem
Dim pubCal As Folder
Set olApp = CreateObject("Outlook.Application")
Set pubCal = GetPublicFolder(olApp, "All Public Folders\Big Store A Calendar")
pubCal.Display
Set olAppItem = Nothing
Set olApp = Nothing
Set pubCal= Nothing
End Sub
Public Function GetPublicFolder(objApp, strFolderPath)
Dim colFolders
Dim objFolder
Dim arrFolders
Dim i
On Error Resume Next
strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders = Split(strFolderPath, "\")
Set objFolder = objApp.Session.GetDefaultFolder(18) 'This is the correct folder # for “All Public Folders”
Set objFolder = objFolder.Folders.Item(arrFolders(0))
If Not objFolder Is Nothing Then
For i = 1 To UBound(arrFolders)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arrFolders(i))
If objFolder Is Nothing Then
Exit For
End If
Next
End If
Set GetPublicFolder = objFolder
Set colFolders = Nothing
Set objApp = Nothing
Set objFolder = Nothing
End Function

Resources