Subject: RE: Rule for getting spam to junk mail folder
I have created a script that copies a rule from a central database and activates it in the users mailbox.
Most of the script is copied from the rules scriptlibrary and modified for my special purpose.
The following script works after the rules is copied to the users maildatabase. It is run from a button in a mail sent to the end user.
It activates the rule that has [spam] as a condition but this can be altered.
Dim db As NotesDatabase
Sub Click(Source As Button)
On Error Goto eh01
Dim uiw As New notesuiworkspace
Dim v As NotesView
Dim regel As NotesDocument
Dim uid As NotesUIDocument
Set db = uiw.CurrentDatabase.Database
Set v = db.GetView("(Rules)")
Call v.Refresh
Set regel = v.GetFirstDocument
While Not regel Is Nothing
If Instr(regel.getitemvalue("ConditionList")(0), "[spam]") > 1 Then
Call deaktiver(regel)
Call aktiver(regel)
Exit Sub
End If
Set regel = v.GetNextDocument(regel)
Wend
Msgbox "Kunne ikke finde [spam] regelen ! Kontakt IT-Drift",, "Fejl"
eh01exit:
Exit Sub
eh01:
Msgbox "Fejl under afvikling af " + Cstr(Getthreadinfo(1)) + " : " + Cstr(Err) + " " + Error(Err) + " i linie " + Cstr(Erl)
Resume eh01exit
End Sub
Sub deaktiver(note As NotesDocument)
On Error Goto eh02
Dim folder As NotesView
Dim vrulenum As Variant
Dim strfilteritem As String
Dim profile As notesdocument
Dim x As Integer
Set profile = db.GetProfileDocument("CalendarProfile")
If note.getitemvalue("Enable")(0) <> "1" Then
Msgbox("Reglen/Reglerne er allerede deaktiveret.")
Exit Sub
End If
vrulenum = note.getitemvalue("ordernum")(0)
strfilteritem = "$FilterFormula_"+Cstr(vrulenum)
If Not(profile Is Nothing) Then
Call profile.removeitem(strfilteritem)
Call profile.save(True, True,True)
Call note.replaceitemvalue("Enable","0")
Call note.save(True,False,True)
Else
Msgbox("Kan ikke finde profilen. Reglen kan ikke deaktiveres.")
Exit Sub
End If
eh02exit:
Exit Sub
eh02:
Msgbox "Fejl under afvikling af " + Cstr(Getthreadinfo(1)) + " : " + Cstr(Err) + " " + Error(Err) + " i linie " + Cstr(Erl)
Resume eh02exit
End Sub
Function FindHighestRule() As Integer
Dim s As New Notessession
Dim folder As NotesView
Dim vec As Notesviewentrycollection
Dim e As Notesviewentry
Dim highest As Integer
Set folder = s.Currentdatabase.Getview("(Rules)")
If folder Is Nothing Then Exit Function
Highest = 0
Set vec = folder.Allentries
If vec.count > 0 Then
Set e = vec.Getfirstentry
While Not e Is Nothing
If highest < e.Columnvalues(1) Then
highest = Cint(e.Columnvalues(1))
End If
Set e = vec.Getnextentry( e )
Wend
End If
If highest = 0 Then
highest = 1
End If
FindHighestRule = highest
End Function
Sub aktiver(note As NotesDocument)
On Error Goto eh03
Dim vrulenum As Variant
Dim strfilteritem As String
Dim profile As notesdocument
Dim item As notesitem
Set profile= db.GetProfileDocument("CalendarProfile")
If note.getitemvalue("Enable")(0) = "1" Then
Msgbox("Reglen/Reglerne er allerede aktiveret.")
Exit Sub
End If
vrulenum = note.getitemvalue("ordernum")(0)
'// $FilterFormulaCount is the total number of rules in the db + 1
Call profile.replaceitemvalue("$FilterFormulaCount", Cstr(FindHighestRule() + 1))
strfilteritem = "$FilterFormula_"+Cstr(vrulenum)
If Not(profile Is Nothing) Then
Set item=note.getfirstitem("$FilterFormula")
If Not(item Is Nothing) Then
' test for the existence of the $FilterFormula_x - if it is there, remove it
If Not(profile.hasitem(strfilteritem)) Then
Call item.copyitemtodocument(profile,strfilteritem)
Else
Call profile.removeitem(strfilteritem)
Call item.copyitemtodocument(profile,strfilteritem)
End If
Call note.replaceitemvalue("Enable","1")
' save the note
Call note.save(True,False,True)
End If
Else
Msgbox("Kan ikke finde profilen. Reglen kan ikke aktiveres.")
Exit Sub
End If
Call profile.save(True, True,True)
eh03exit:
Exit Sub
eh03:
Msgbox "Fejl under afvikling af " + Cstr(Getthreadinfo(1)) + " : " + Cstr(Err) + " " + Error(Err) + " i linie " + Cstr(Erl)
Resume eh03exit
End Sub