Outlook .StartDate & .EndDate Sometimes Work

nick34

New Member
Joined
May 6, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
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 (I am not claiming credit for this, a lot was copied from others work). 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
'.Display
SendKeys "{TAB}" & "{TAB}" & "{TAB}" & "{DEL}" & "{DEL}"
.Recipients.Add "staff@xyzcompany.com"
.Subject = "EODR " & Format(Date, "mm-dd-yyyy")
.Attachments.Remove (1)
.Send
End With

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

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,214,788
Messages
6,121,582
Members
449,039
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top