Early, I posted theme "Groovy: autosave attachment with specific extension from Lotus Notes", but couldn't find any solution, so i found different vbscripts and make own. Its works, but now i have a problem: letter doesn't delete after as script autosave attach. I see error: "All objects must be from the same session". I will be grateful for any idea.
Dim Session
Dim Maildb
Dim vw
Dim doc
Dim Item
Dim x
Set Session = CreateObject("Lotus.NotesSession")
Call Session.Initialize("password")
Set Maildb = Session.GetDatabase("SERVER", "mail.nsf")
If Not Maildb.IsOpen = True Then
Call Maildb.Open
End If
Set vw = Maildb.GetView("($inbox)")
With vw
x = 0
ReDim LmailID(x)
ReDim HasAttach(x)
Set doc = .GetFirstDocument
Set Item = doc.GetFirstItem("Body")
Do
If Item.Type = RICHTEXT Then - here i try take unread message and it doesnt work
fileNames = Session.Evaluate("@AttachmentNames", doc)
For Each Filename In fileNames
If Filename <> "" Then
If Right(Filename, 3) = "bch" Then
Call doc.Save( False, True, True )
Set NotesEmbeddedObject = doc.GetAttachment(Filename)
NotesEmbeddedObject.ExtractFile ("C:\" + Filename)
Set reply = doc.CreateReplyMessage( False )
Call reply.replaceItemValue("Subject", "DONE" + subject)
Call reply.Send( False )
Set nextDoc = .GetNextDocument(doc)
Set doc = nextDoc
End If
End If
Next
End If
x = x + 1
ReDim Preserve LmailID(x)
Set doc = .GetNextDocument(doc)
Wscript.Sleep 500
Loop Until doc Is Nothing
End With
Set Session = Nothing
Set vw = Nothing
Set doc = Nothing
Set Item = Nothing
Set Maildb = Nothing
Some hints:
Inside the loop you are using .GetFirstDocument
. Move that outside the loop (outside With vw
) and then use .GetNextDocument(doc)
inside the loop (as you are already doing).
Also, you are calling doc.Remove(True)
and then later trying to fetch the next document by refering to the doc
instance that you just deleted. To fix this, you can add another document instance such as nextDoc
and then use this as a temporary document while you delete the doc
instance:
Before you delete doc:
Set nextDoc = .GetNextDocument(doc)
After you have deleted doc:
Set doc = nextDoc