Subject: How do I go to next document on error
Nin Hao Jennifer,
You could structure your code into subroutines and add a noteslog as error destination.
'Please for heavens sake:
OPTION DECLARE
Sub Initialize
Dim db as Notes DataBase
Dim doc as NotesDocument
Dim v as NotesView
Dim nlog as NotesLog
'Code starts here
On Error Goto Err_MyAgent
Dim s as New NotesSession
Set db = s.currentDatabase
Set nlog = new NotesLog("My Agent")
nlog.logErrors = true
Call nlog.OpenMaillog("my email',"Results of MyAgent") 'Typically this would be a mail-in databas
Set v = db.Getview("YourViewName')
Set doc = v.GetFirstDocument()
Do until doc Is Nothing
Call WorkOnThisDoc(doc, nlog)
Set doc = v.GetNextDocument(doc)
End Do
Exit_MyAgent:
On Error Resume Next
nlolg.close
Exit sub
Err_MyAgent:
if not nlog is nothing then
call nlog.logerror(err,error$+" in MyAgent")
end if
Resume Exit_MyAgent 'Make it a rule --- if code is in exit
End Sub
Sub WorkOnThisDoc(doc as NotesDocument, nlog as NotesLog)
Dim sourceto as String
On Error Goto Err_WorkOnThisDoc
if not doc.hasitem(“SendTo”) then
goto Exit_WorkOnThisDoc 'Goto is acceptable because it shortens the if statement
end if
sourceto = doc.SendTo(0)
If Instr(sourceto, “sender”) = 0 Then
Call doc.CopyToDatabase(copydb)
Print "Copied one document from " + db.Title
Call Nlog.logaction("Copied one document from " + db.Title)
End If
Exit_WorkOnThisDoc:
Exit Sub
Err_WorkOnThisDoc:
call nlog.logerror(err,Error$+" in WorkOnThisDoc")
Resume Exit_WorkOnThisDoc
End Sub
The code might contain typos since I just typed it of memory.
Important concepts here:
Option declare
make a loop short – just call a subroutine/function inside
Use Do instead of While
Use a NotesLog for error logging
Use a consitent schem for error handling: for the initilize the labels are the agent name with Err_ and Exit_ in front, for subs/functions the sub/function name
Hope that helps
stw