Script Modification

Can someone add on to this script so that when it is done it will delete the selected documents?

      On Error Goto ErrorHandler
      On Error 183 Goto BadFromField      
      On Error 184 Goto BadFromField
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Set db = session.CurrentDatabase
      Dim view As NotesView
      Set view = db.GetView("AntiSpam")
      Dim documents As NotesDocumentCollection
      Set documents = db.UnprocessedDocuments
      Dim configdoc As NotesDocument
      Dim doc As NotesDocument
      Dim counter As Integer, FromField As String, startpos As Integer, endpos As Integer
REM Loop through selected memos to create persistent SpamCFG docs
      Set doc = documents.GetFirstDocument
      Do Until doc Is Nothing
            If doc.DeliveredDate(0)<>"" Then      ' it is indeed a memo I received vs. sent
                  Counter = Counter + 1  
                  Set configdoc = db.CreateDocument
                  getFromField = Evaluate("@Text(From)", doc)
                  FromField = getFromField(0)
                  Dim fromname As New NotesName(FromField)
                  internetaddress = fromname.Addr821                  
                  If internetaddress= "" Then ' must be Notes mail address
                        If doc.FromDomain(0) = "" Then  ' must be from my notes domain
                              getdomain = Evaluate("@Domain", doc)  
                              domain = getdomain(0)
                              getfrom =  fromname.abbreviated ' don't add domain to name
                        Else ' must be from another notes domain
                              domain = doc.FromDomain(0)
                              getfrom =  fromname.abbreviated & "@" & domain
                        End If
                  Else  ' it is an internet address
                        getfrom = FromField
                        pos = Instr(internetaddress, "@")
                        domain = Mid(internetaddress, pos+1)
                  End If
                  configdoc.From = getfrom
                  configdoc.Domain = "@" & domain
                  configdoc.IndividualOrDomain = "0"  ' set default to individual name
                  configdoc.Form = "SpamCFG"
                  REM do not want SpamCFG to show up in views other than AntiSpam
                  Dim ExcludeViews(1)
                  ExcludeViews(0) = "A"
                  ExcludeViews(1) = "D"
                  configdoc.ExcludeFromView = ExcludeViews
                  configdoc.HiddenDocument = "1"
                  REM save AntiSpam doc
                  Call configdoc.Save(True, False)
            End If                              
            Set doc = documents.GetNextDocument (doc)
      Call View.Refresh
      Msgbox Counter & " addresse(s) added out of  " & documents.count & " selected."
      Exit Sub
      Msgbox "Error " & Err() & ":  " & Error() & ".  Some addresses may not have been added."
      Exit Sub
      Msgbox "Error - From field contains unrecognizable characters.  Some addresses may not have been added."
      Goto TryAgain
      Exit Sub  

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi schmad01,


> TryAgain:
>           Set doc = documents.GetNextDocument (doc)


          Dim nextDoc as notesDocument
          Set nextDoc = documents.GetNextDocument (doc)
          Set doc = nextDoc

What this does:

For each document you process, after you have finished processing, you normally get the next document.  So, we want to put in a doc.remove before we move on to the next document.  The only problem is, once we remove the doc, we have no way of using the (now dearly departed) doc object to find the next doc.  So, we briefly use a temp doc object, nextDoc, to get the next document BEFORE we delete the previous (current) one.  Then, once we remove the current one, we assign the doc object, which is used everywhere, to the value of getNextDocument that was already pre-retrieved into nextDoc.

Best regards,

Simply after the loop statement add documents.RemoveAll(true)


Call documents.RemoveAll(true) ' This will remove whole collection when it is done.
Call View.Refresh

But if you want to delete only selected docs which are processed as spam then you might have to create a new collection and add those docs which are spam and then remove the collection at the end.

Good call, Hemantha.  The deletiondoes not have to be synchronous to the processing, and this eliminates the loop.  I forot about RemoveAll
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

schmad01Author Commented:
Ok, on qwal's suggestion I get:
57: Missing argument for :REMOVE

On Heman's I get:
Error 4241: Cannot remove NotesDocument when instantiated by NotesUIDocument. Some addresses may not have been added.

Any thoughts?
Well it is weird, I don't see any NotesUIDocument in your script. Is this script run from an agent ? The collection is totally from background..isn't it ?
          Dim nextDoc as notesDocument
          Set nextDoc = documents.GetNextDocument (doc)
          doc.remove true
          Set doc = nextDoc

But you could get the same error as Hemantha's code gives... if the open document is part of the UnprocessedDocuments collection that you are processing.

So, please explain what's going on here.  Since you are using UnprocessedDocumnts, I assuemd that this is in an agent.  Are you calling the agent from a button or hotspot or action bar on a form?  In that case, you shoudl close the document before running the agent.  Otherwuse,  can give you a workaround that will exclude the current document, but I still need info on exactly how the code is being called.
schmad01Author Commented:
Yes, this is an agent.  And, I am running it from the preview window which may account for the doc being open problem. I would like a workaround for this as I use the document preview all the time.  Thanks.
ok that explains the error.

Right now there is no workaround, except to close the preview pane. So call the agent in conjunction with commands in view action buton

@Command([ShowHidePreviewPane]; "0");
@Command([ToolsRunMacro]; "Agent Name");
@PostedCommand([ShowHidePreviewPane]; "1" );

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

hate to contradict Hemanth, but there is one other workaround.

You can delete all other documents besides the one that is open.  The question then becomes, what to do about the open one?  I believe you can delete it through the front end classes.

Would you like to work through this possibility?
schmad01Author Commented:
Opening and closing of the preview pane works just great!  Thanks again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.