Using Restrict with a Date Range

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to restrict the Outlook Calendar Items to only the appointments that fall into a specified date range. However, the following code always gives me a "tyoe mismatch or "beginning_date" is not valid"

Could someone show me how to change this to use restrict with a date range

Dim ol As New Outlook.Applicatio
Dim olns As Outlook.Namespac
Dim cf As Outlook.MAPIFolde
Dim c As Outlook.AppointmentIte
Dim objItems As Outlook.Item
Dim objItemsRestricted As Outlook.Item
Dim Prop As Outlook.UserPropert
Dim Beginning_Date As Date, Ending_Date As Dat

Set olns = ol.GetNamespace("MAPI"
Set cf = olns.GetDefaultFolder(olFolderCalendar
Set objItems = cf.Item
Beginning_Date = #2/1/2004
Ending_Date = #3/30/2004 11:59:59 PM
objItems.IncludeRecurrences = Tru
Set objItemsRestricted = objItems.Restrict("[Start] >= Beginning_Date and [End] <= Ending_Date"
 
Beginning_Date and Ending_Date are used as strings rather than variables,
obviously Outlook does not know what they mean:\

Set objItemsRestricted = objItems.Restrict("[Start] >= '" & Beginning_Date &
"' and [End] <= '" & Ending_Date & "'")

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Ken Edwards said:
I am trying to restrict the Outlook Calendar Items to only the
appointments that fall into a specified date range. However, the following
code always gives me a "tyoe mismatch or "beginning_date" is not valid".
Could someone show me how to change this to use restrict with a date range?

Dim ol As New Outlook.Application
Dim olns As Outlook.Namespace
Dim cf As Outlook.MAPIFolder
Dim c As Outlook.AppointmentItem
Dim objItems As Outlook.Items
Dim objItemsRestricted As Outlook.Items
Dim Prop As Outlook.UserProperty
Dim Beginning_Date As Date, Ending_Date As Date

Set olns = ol.GetNamespace("MAPI")
Set cf = olns.GetDefaultFolder(olFolderCalendar)
Set objItems = cf.Items
Beginning_Date = #2/1/2004#
Ending_Date = #3/30/2004 11:59:59 PM#
objItems.IncludeRecurrences = True
Set objItemsRestricted = objItems.Restrict("[Start] >= Beginning_Date and [End] <= Ending_Date")
 
The Find and Restrict methods require that the date and time be converted to
a string representation:

Dim ol As New Outlook.Application
Dim olns As Outlook.NameSpace
Dim cf As Outlook.MAPIFolder
Dim c As Outlook.AppointmentItem
Dim objItems As Outlook.Items
Dim objItemsRestricted As Outlook.Items
Dim Prop As Outlook.UserProperty
Dim Beginning_Date As Date, Ending_Date As Date
Dim strBegin As String, strEnd As String

Set olns = Application.GetNamespace("MAPI")
Set cf = olns.GetDefaultFolder(olFolderCalendar)
Set objItems = cf.Items
Beginning_Date = #2/1/2004#
Ending_Date = #3/30/2004 11:59:59 PM#
objItems.IncludeRecurrences = True
strBegin = Format(Beginning_Date, "ddddd h:nn AMPM")
strEnd = Format(Ending_Date, "ddddd h:nn AMPM")
Set objItemsRestricted = objItems.Restrict("[Start] >= '" & strBegin & "
and [End] <= " & strEnd & "'")

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/


Ken Edwards said:
I am trying to restrict the Outlook Calendar Items to only the
appointments that fall into a specified date range. However, the following
code always gives me a "tyoe mismatch or "beginning_date" is not valid".
Could someone show me how to change this to use restrict with a date range?

Dim ol As New Outlook.Application
Dim olns As Outlook.Namespace
Dim cf As Outlook.MAPIFolder
Dim c As Outlook.AppointmentItem
Dim objItems As Outlook.Items
Dim objItemsRestricted As Outlook.Items
Dim Prop As Outlook.UserProperty
Dim Beginning_Date As Date, Ending_Date As Date

Set olns = ol.GetNamespace("MAPI")
Set cf = olns.GetDefaultFolder(olFolderCalendar)
Set objItems = cf.Items
Beginning_Date = #2/1/2004#
Ending_Date = #3/30/2004 11:59:59 PM#
objItems.IncludeRecurrences = True
Set objItemsRestricted = objItems.Restrict("[Start] >= Beginning_Date and [End] <= Ending_Date")
 
Plus, you can't use seconds. And, to catch items that overlap the desired
range, you actually have to change the operators to look for items that
start before your [End] date and end after your [Start] date. See
http://www.outlookcode.com/d/finddate.htm
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Dmitry Streblechenko (MVP) said:
Beginning_Date and Ending_Date are used as strings rather than variables,
obviously Outlook does not know what they mean:\

Set objItemsRestricted = objItems.Restrict("[Start] >= '" & Beginning_Date &
"' and [End] <= '" & Ending_Date & "'")


Ken Edwards said:
I am trying to restrict the Outlook Calendar Items to only the
appointments that fall into a specified date range. However, the following
code always gives me a "tyoe mismatch or "beginning_date" is not valid".
Could someone show me how to change this to use restrict with a date range?

Dim ol As New Outlook.Application
Dim olns As Outlook.Namespace
Dim cf As Outlook.MAPIFolder
Dim c As Outlook.AppointmentItem
Dim objItems As Outlook.Items
Dim objItemsRestricted As Outlook.Items
Dim Prop As Outlook.UserProperty
Dim Beginning_Date As Date, Ending_Date As Date

Set olns = ol.GetNamespace("MAPI")
Set cf = olns.GetDefaultFolder(olFolderCalendar)
Set objItems = cf.Items
Beginning_Date = #2/1/2004#
Ending_Date = #3/30/2004 11:59:59 PM#
objItems.IncludeRecurrences = True
Set objItemsRestricted = objItems.Restrict("[Start] >= Beginning_Date and [End] <= Ending_Date")
 
Thanks

I had tried to concant all parts of the expression into a string variable, and then use the string variable, but then I got a different error. Your code inserted gives the same error

Run-time error '0313393143 (ed520009)'
Type mismatch or the value "3/30/2004 23:59:59" in the condition is not valid

I see nothing wrong with the date format. Both [Start] and [End] are dates. I don't know what to do.
 
Back
Top