Restrict to Today's Received Mail in VBA

  • Thread starter Thread starter Sriram N A \(MICO/PJ-SAP-PP\) *
  • Start date Start date
S

Sriram N A \(MICO/PJ-SAP-PP\) *

I have code in the ThisOutlookSession module which attempts to process the
current day's mail looking for a subject which begins with a number. If a
matching Distribution List is found with the same name, the item is
forwarded to the DL (There are reasons why I cannot use an auto-forward
rule).

The problem is, the Restrict method appears not be catching all instances of
"Today's mail" reliably. Is there anything wrong with this code?:

Sub SendASN()

Dim myFolder As MAPIFolder

Dim sFilter As String

Dim ItemsToProcess As Outlook.Items

Dim myItem As MailItem, myForwardItem As MailItem

Dim MyRecipient As String



'Scan through today's mail in the Inbox.

'Check whether each mail is possibly an ASN

'and forward to the appropriate contact DL



Set myFolder = Session.GetDefaultFolder(olFolderInbox)

sFilter = "[ReceivedTime] >= " & AddQuotes(Format(Date, "ddddd"))

Set ItemsToProcess =
Session.GetDefaultFolder(olFolderInbox).Items.Restrict(sFilter)

For Each myItem In ItemsToProcess

MyRecipient = Left(Replace(myItem.Subject, "FW: ", "", , 1), 6)

If IsNumeric(MyRecipient) And myItem.Attachments.Count > 0 And
CheckContact(MyRecipient) Then

Set myForwardItem = myItem.Forward

'Do my thing

End If

Next

End Sub
 
Without seeing the code for AddQuote()... The quotes
should be a single quote.

As in the example:

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

Also, if you are doing something with the item in the
inbox, such as moving, deleting, etc. Or
otherwise "modifying" the collection defined in your
For...Each loop, that can give you unexpected results too.

I very recently encountered this logical error myself.
 
Well, AddQuotes does use single quotes:

Private Function AddQuotes(MyText) As String
AddQuotes = Chr(34) & MyText & Chr(34)
End Function

And I am invoking the Forward action on eligible items, which shouldn't
affect the collection.

My doubt is whether the date format is correct, since this is a string
comparison? And is the Filter method reliable when filtering on date?

Sriram
 
Chr(34) is a quote character (double quote), not a single quote
(apostrophe).

See the Object Browser Help file example on Restriction to see some examples
using a filter with dates.
 
I'm quoting from the Example for the Restrict method in the Help file:

Using Variables as Part of the Filter
As the Restrict method example illustrates, you can use values from
variables as part of the filter. The following Microsoft Visual Basic
Scripting Edition (VBScript) code sample illustrates syntax that uses
variables as part of the filter.

sFullName = "Dan Wilson"

' This approach uses Chr(34) to delimit the value.

sFilter = "[FullName] = " & Chr(34) & sFullName & Chr(34)

' This approach uses double quotation marks to delimit the value.

sFilter = "[FullName] = """ & sFullName & """"

I took the chr(34) approach. Any problems there?
 
As long as you use the correct number of quote characters so the string is
parsed correctly it doesn't matter at all if you use the literal quote
character or Chr(34). Chr(34) is a double-quote however, not a single quote
as the earlier post mistakenly said and that's what I was correcting.
 
Back
Top