I am using the following code to search the subject field of the document in memory for the string “SPAM”, and then move the document to the Junk folder. The problem is, if I have a document with “SPAM” anywhere in the subject, nothing happens to the document, it shows up as not a match. What am I doing wrong?
Dim s As New NotesSession
Dim doc As NotesDocument
Dim spam As NotesItem
Set db = s.CurrentDatabase
Set doc = s.Documentcontext
'Messagebox doc.Subject(0)
Set spam = doc.GetFirstItem("Subject")
If spam.Text Like "SPAM" Then
Call doc.PutInFolder( "$JunkMail" )
Call doc.Removefromfolder("$Inbox")
End If
Subject: Search subject field for string, and move to folder
You are trying to match precisely the word “SPAM” to the subject. If the subject contains anything else other than “SPAM” (for example, “SPAM → Enlargements R Us”) then it will not match.
You need some wildcards in your comparison i.e.
If spam.Text Like “SPAM*” Then
Subject: RE: Search subject field for string, and move to folder
Will that work, even if there are some characters before the word spam?
Subject: RE: Search subject field for string, and move to folder
You have to put wild cards in the places where you expect the other characters to be. If there will be other characters in front of the word “SPAM”, then you should add wildcards there too. However…
Sub Click(Source As Button)
Dim spam As String
spam = "This is spam - you see?"
Msgbox spam Like "SPAM" 'False
Msgbox spam Like "SPAM*" 'False
Msgbox spam Like "*SPAM" 'False
Msgbox spam Like "*SPAM*" 'False
Msgbox spam Like "*[Ss][Pp][Aa][Mm]*" 'True
spam = "I love spamoni ice cream" 'OK, I know it's not spelled that way
Msgbox spam Like "SPAM" 'False
Msgbox spam Like "SPAM*" 'False
Msgbox spam Like "*SPAM" 'False
Msgbox spam Like "*SPAM*" 'False
Msgbox spam Like "*[Ss][Pp][Aa][Mm]*" 'True
End Sub
Subject: RE: Search subject field for string, and move to folder
Thanks so much, it worked. So the way I had it, it was looking for any character s, p, a, m in the field?
Subject: RE: Search subject field for string, and move to folder
…and only those characters (actually, the upper case version, since you were comparing to “SPAM”)