none
Restrict received time in email item in vbscript RRS feed

  • Question

  • I got the below error when I want to restrict the received time over 3 days old, please help. Many thanks.

      startTime = FormatDateTime(DateAdd("d", -3, Date()), vbGeneralDate)
      restriction = " [ReceivedTime] > '" & startTime

    Error: Cannot parse condition.Error at "15/04/2016".
    Source: Microsoft Outlook.

    Monday, April 18, 2016 4:18 AM

Answers

  • Hi 

    In your code,

    FormatDateTime(DateAdd("d", -3, Date()), vbGeneralDate) function will return the value as 

    "19/04/2016", since the Date() function will return only the Date part and not the time.

    can you try this

    FormatDateTime(DateAdd("d", -3, Now()), vbGeneralDate)

    Instead of Date() function use the Now() function which will return the system date time with am/pm indicators based on the System-wide date time settings

    VB FormatDateTime Function

    Hope this helps

    Raghu



    Tuesday, April 19, 2016 1:13 PM

All replies

  • Hello,

    You need to use the date and time format which Outlook can understand.

    Although dates and times are typically stored with a Date format, the Find and Restrict methods require that the date and time be converted to a string representation. To make sure that the date is formatted as Microsoft Outlook expects, use the Format function. The following example creates a filter to find all contacts that have been modified after January 15, 1999 at 3:30 P.M.

    sFilter = "[LastModificationTime] > '" & Format("1/15/99 3:30pm", "ddddd h:nn AMPM") & "'"

    For example, take a look at the following article which provides a sample code:

    Sub FindApptsInTimeFrame()
        Dim myStart, myEnd As Date
        Dim oCalendar As Outlook.Folder
        Dim oItems As Outlook.items
        Dim oResItems As Outlook.items
        Dim oAppt As Outlook.AppointmentItem
        Dim strRestriction As String
        
        myStart = Format(Date, "mm/dd/yyyy hh:mm AMPM")
        myEnd = DateAdd("d", 5, myStart)
        myEnd = Format(myEnd, "mm/dd/yyyy hh:mm AMPM")
        Debug.Print "Start:", myStart
        Debug.Print "End:", myEnd
        
        Set oCalendar = Application.Session.GetDefaultFolder(olFolderCalendar)
        Set oItems = oCalendar.items
        
        oItems.IncludeRecurrences = True
        oItems.Sort "[Start]"
        
        strRestriction = "[Start] <= '" & myEnd _
        & "' AND [End] >= '" & myStart & "'"
        Debug.Print strRestriction
        
        'Restrict the Items collection
        Set oResItems = oItems.Restrict(strRestriction)
        'Sort
        oResItems.Sort "[Start]"
        
        For Each oAppt In oResItems
            Debug.Print oAppt.Start, oAppt.Subject
        Next
    End Sub

    How to: Search the Calendar for Appointments that Occur Partially or Entirely in a Given Time Period


    [custom.development]

    Monday, April 18, 2016 8:43 AM
  • Error: Variable is undefined: 'Format'

    Source: Microsoft VBScript runtime error

    Monday, April 18, 2016 9:19 AM
  • Looks like VBScript doesn't provide the Format function. Try to run the code in the VBA editor. Thus, you will be sure what date and time format should be used. You simply need to format the date and time using the format Outlook can understand. 

    [custom.development]

    Monday, April 18, 2016 10:09 AM
  • Hi 

    In your code,

    FormatDateTime(DateAdd("d", -3, Date()), vbGeneralDate) function will return the value as 

    "19/04/2016", since the Date() function will return only the Date part and not the time.

    can you try this

    FormatDateTime(DateAdd("d", -3, Now()), vbGeneralDate)

    Instead of Date() function use the Now() function which will return the system date time with am/pm indicators based on the System-wide date time settings

    VB FormatDateTime Function

    Hope this helps

    Raghu



    Tuesday, April 19, 2016 1:13 PM