.StartDate & .EndDate Only Works Sometimes


New Member
Outlook version
Outlook 2019 32-bit
Email Account
Office 365 Exchange
The goal is to email my Calendar every day, along with the previous days for that week. For example, on Thursday - send Monday through Thursday. It usually works fine, but some days it sends the current date and way too many previous days. Attached is the program Could someone please help me figure out why this works some days and doesn't other days? Here is the program:

Public Sub SendCalendar()

Dim oNamespace As NameSpace
Dim oFolder As Folder
Dim oCalendarSharing As CalendarSharing
Dim objMail As MailItem
Dim dtToday As Date
Dim dtOneDayAgo As Date
Dim dtTwoDaysAgo As Date
Dim dtThreeDaysAgo As Date
Dim dtFourDaysAgo As Date
Dim iWeekday As Integer

Set oNamespace = Application.GetNamespace("MAPI")
Set oFolder = oNamespace.GetDefaultFolder(olFolderCalendar)
Set oCalendarSharing = oFolder.GetCalendarExporter

dtToday = Date
dtOneDayAgo = DateAdd("d", -1, Date)
dtTwoDaysAgo = DateAdd("d", -2, Date)
dtThreeDaysAgo = DateAdd("d", -3, Date)
dtFourDaysAgo = DateAdd("d", -4, Date)

iWeekday = Weekday(Date)

If iWeekday = 2 Then
lDays = dtToday
ElseIf iWeekday = 3 Then
lDays = dtToday And dtOneDayAgo
ElseIf iWeekday = 4 Then
lDays = dtToday And dtTwoDaysAgo
ElseIf iWeekday = 5 Then
lDays = dtToday And dtThreeDaysAgo
ElseIf iWeekday = 6 Then
lDays = dtToday And dtFourDaysAgo
End If

With oCalendarSharing
.CalendarDetail = olFreeBusyAndSubject
.IncludeWholeCalendar = False
.IncludeAttachments = False
.IncludePrivateDetails = True
.RestrictToWorkingHours = False
.StartDate = dtFourDaysAgo
.EndDate = dtToday
End With

Set objMail = oCalendarSharing.ForwardAsICal(olCalendarMailFormatDailySchedule)

With objMail
SendKeys "{TAB}" & "{TAB}" & "{TAB}" & "{DEL}" & "{DEL}"
.Recipients.Add "staff@xyzcompany.com"
.Subject = "EODR " & Format(Date, "mm-dd-yyyy")
.Attachments.Remove (1)
End With

Set oCalendarSharing = Nothing
Set oFolder = Nothing
Set oNamespace = Nothing

End Sub

Thread starter Similar threads Forum Replies Date
U Keyboard Remapper no longer works in Outlook Using Outlook 1
S Macro using .SendUsingAccount only works the first time, after starting Outlook Outlook VBA and Custom Forms 4
R Email account works on thunderbird not Outlook Using Outlook 6
T 2 mostly identical scripts, only one works Using Outlook 16
David Michaels VB coding works in OL 2010, but not in OL 2013 Outlook VBA and Custom Forms 4
D Default Send Account that Works? Using Outlook 0
J Cached mode no longer works with NTLM authentication Exchange Server Administration 0
S my vbscript button1_click code works on appointment created but not on opening an existing apntmn Outlook VBA and Custom Forms 16
R SEARCH PEOPLE - would like one that really works Using Outlook 1
B Display of Sending Account in Outlook 2007 no longer works Using Outlook 2
S Outlook no longer works with Yahoo Mail Using Outlook 9
E Need a bit of education on how Outlook works please Using Outlook 3
P Outlook works, but freezes when trying to use calender v2007 (vista prof) Using Outlook 1
B Outlook connector only works when set to default Using Outlook.com accounts in Outlook 5
H Two accounts, Junk email filter only works on one account, office 2010, pop3 Using Outlook 5
D VBA Macro Works in 2007 but errors in 2010 Using Outlook 2
X Outlook 2010 - search works on one system but not other - identical configurat Using Outlook 2
N Filter end date works differently in OL'07/OL'10 vs OL'03? Hello?! Using Outlook 12
M Attachments cannot be opened in Outlook, OWA works fine Using Outlook 2
J Outlook Connector only works for a day?! Using Outlook.com accounts in Outlook 3
W Conditional Formatting - Message Body only works to about 200 characters Using Outlook 2
K Does a custom sharepoint IRM protector works on Exchange 2010? Exchange Server Administration 0
B Outlook 2007 New email notification works intermittently after adding Exchange 2010 Servers Exchange Server Administration 1
I Outlook can't connect but OWA works just fine (Recovery Mode Error Message) Exchange Server Administration 2
I My "DELETE" key does not work when I am in my Outlook email but works when in other emails such as G Using Outlook 2
I I cannot open Microsoft Word attachments - Message: Works cannot open... Using Outlook 1
S smtp error when password changed but works again with old password Using Outlook 1
H Outlook 2007 periodically disconnects from Exchange 2010 CAS - OWA works Using Outlook 4
E No free/busy info could be retrieved via RPC, works with VPN Exchange Server Administration 4
D Outlook crashes when trying to connect to Exchange, but works fine in safe mode Using Outlook 1
B Upgraded to Outlook 2007, my custom stationary no longer works Using Outlook 1
J outlook will not open, uploaded new 2010 to replace beta version and everything but outlook works. ____ Using Outlook 4
D Mouse does not function in the message area of Outlook 2007, works fine elsewhere Using Outlook 1
L Can not get Outlook to save e-mail account passwords. If I put passwords in manually system works fine, but when I close Outlook and reopen I have to Using Outlook 1
F Active Sync proxying not works in a Exchange 2010/2007 Coexistence Exchange Server Administration 1
N Outlook cannot find users; Web Access works Using Outlook 13
K Everything from my office suite 2010 works except Outlook...It says (Not responding) no matter what I click on. Using Outlook 1
L Outlook 2010 test message works, but emails don't go out Using Outlook 2
R Password not accepted but works when saved. Using Outlook 3
S EMC 2010 will not connect , EMS 2010 will not connect.... Exchange works but cannot be administered HELP HELP HELP Exchange Server Administration 3
J Outlook 2010 shuts down and quickly restarts every time I try to attach a file, but otherwise works Using Outlook 2
J Outlook won't allow hyperlink in email to work, but copying & posting hyperlink in browser works OK. Using Outlook 1
T Unable to edit Distribution Group membership via Outlook (works via ECP). Exchange Server Administration 7
G Outlook not SENDING HTML formatted email; Thunderbird works fine; same pop/smtp servers Using Outlook 1
M OWA client works, Outlook client gets 'name cannot be resolved' Using Outlook 3
D get-logonstatistics works differently in E2K10 Exchange Server Administration 4
P Exchange Webmail no longer works Exchange Server Administration 4
D I need a fax server that works with SharePoint 2010 Exchange Server Administration 2
J Outlook Connector No Longer Works Using Outlook 3
W SP1 mgmt tools, EMS works, EMC initialization failed Exchange Server Administration 12
Similar threads