.StartDate & .EndDate Only Works Sometimes

Not open for further replies.


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

Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
KeithJB iCloud calendar sync only works one way! Using Outlook 3
V Outlook 2021 Outlook-2021 (64-bit) and iCloud-for-Windows v13.0 - It Works Good. Using Outlook 9
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0
A Manually Building DisplayName <emailaddr> No Longer Works Using Outlook 0
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
R Password not accepted but works when saved. Using Outlook 3
T Unable to edit Distribution Group membership via Outlook (works via ECP). Exchange Server Administration 7
J Unable to access public folders in Outlook 2007 on Exchange 2010. OWA PF works fine Using Outlook 7
R BCM works, but everything else stopped working BCM (Business Contact Manager) 1
D Using VBA to send Word document as body of message works in 2007, but not in 2003 Outlook VBA and Custom Forms 9
T Works in Word but not Outlook. Plz Help! Outlook VBA and Custom Forms 1
C Copy email to different folder, works in 2003 but not 2007 Outlook VBA and Custom Forms 3
B Email Autolink Works Sporadically BCM (Business Contact Manager) 2

Similar threads