Before new mail arrives problem

The following script detaches the attachments and places them into a folder. I had it working at one point if you opened the message and it currently works on the first email rather than a selected email. I need it to work on an email that triggers the “before new mail arrives” condition. It seems that it does not correctly set the document object. Can anyone point out what I am doing wrong? I have tried with just dim doc as notesdocument, same problem, it seems the document object isn’t correclty set to the email I am trying to deal with.Its a bit of a hack job at the moment hence the somewhat odd code layout.

Sub Initialize

Dim rtitem As Variant

Dim fileCount As Integer    

Const MAX = 1000

fileCount = 0    



Dim workspace As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument

Set uidoc = workspace.CurrentDocument



Set doc = uidoc.Document



'...set value of doc...

Set rtitem = doc.GetFirstItem("Body")







Print "detach attempt"

        

Forall o In rtitem.EmbeddedObjects

    If ( o.Type = EMBED_ATTACHMENT ) And ( o.FileSize > MAX ) Then            

        fileCount = fileCount + 1

        Call o.ExtractFile ( "\\networklocation\remoteshare\" & o.source )

        Call o.Remove

        Call doc.Save( True, True )

    End If

End Forall



Print "detach finished"

End Sub

Subject: RE: before new mail arrives problem

NotesUIWorkspace class cannot be used in before new mail arrives agent which is run by the router.

Use UnprocessedDocument method in the Notesdatabase class.

HTH.

Subject: RE: before new mail arrives problem

Did you take a look at the Agent FAQ? It has articles that will help you! There is an article with sample code of how to write ‘before new mail’ agents, and another one with explanation about not using UI classes when you run in the server agents. Take a look under resources on the right hand side!