Reputation: 1767
Here is a working script for extract attachments from Lotus Notes letters:
Dim s
s = 1
Do
s = s + 1
Dim Session
Dim Maildb
Dim view
Dim vc
Dim doc
Dim Item
Dim coll
Dim x
Dim Sender
Dim sentTo
Dim viewTimer
Dim ws
Dim Source
Set Session = CreateObject("Lotus.NotesSession")
Call Session.Initialize("password")
Set Maildb = Session.GetDatabase("DOMAIN/Servers/Server-Name/RU", "GroupMail\mailbox.nsf")
Set view = Maildb.GetView("($inbox)")
If Not Maildb.IsOpen = True Then
Call Maildb.Open
End If
With view
x = 0
ReDim LmailID(x)
ReDim HasAttach(x)
Set doc = .GetFirstDocument
If doc Is Nothing Then
else
Call doc.PutInFolder("Processed")
Call doc.Removefromfolder("($inbox)")
Do
fileNames = Session.Evaluate("@AttachmentNames", doc)
For Each Filename In fileNames
Sender = doc.GETITEMVALUE("From")(0)
strSearchForSpecificName = "SpecificName"
If InStr(1, Sender, strSearchForSpecificName) > 0 then
sentTo = "SpecificName@mail.ru"
else
sentTo = Sender
End If
If Filename <> "" Then
Call doc.Save( False, False, True )
Set NotesEmbeddedObject = doc.GetAttachment(FileName)
NotesEmbeddedObject.ExtractFile ("d:\#files\" + Right("0" & Month(Now), 2) & "-" & Right("0" & Day(Now), 2) & "-" & Year(Now) & "-" & Hour(Time) & Minute(time) & Second(time) & "_" & Filename)
Set reply = doc.CreateReplyMessage( False )
Call reply.replaceItemValue("SendTo", sentTo)
Call reply.replaceItemValue("CopyTo", "copy@mail.ru")
Call reply.replaceItemValue("Subject", "Re: " & "файл " + Filename + " передан в обработку " + Right("0" & Month(Now), 2) & "-" & Right("0" & Day(Now), 2) & "-" & Year(Now) & Hour(Time) & ":" & Minute(time) & ":" & Second(time))
doc.SaveMessageOnSend = True
Call reply.Send( False )
End If
Next
x = x + 1
ReDim Preserve LmailID(x)
Set doc = .GetNextDocument(doc)
Loop Until doc Is Nothing
End If
End With
Wscript.Sleep (30 * 1000)
Set Session = Nothing
Set Maildb = Nothing
Set view = Nothing
Set vc = Nothing
Set doc = Nothing
Set Item = Nothing
Set coll = Nothing
Set x = Nothing
s = s - 1
Loop While s > 0
The problem is that sometimes I'm getting an error: Error: "Entry not found in index..." and program stopped at Set doc = .GetNextDocument(doc) line. Is there there solution for resolve this error?
Upvotes: 0
Views: 430
Reputation: 14628
Your code is removing the first doc from the $Inbox, therefore it cannot be used as the anchor for getting the next document in $Inbox. The solution would normally be to make sure that you get the next document before you remove the current document. I.e., change
Call doc.PutInFolder("Processed")
Call doc.Removefromfolder("($inbox)")
to
Call doc.PutInFolder("Processed")
set nextDoc = .getNextDocument(doc)
Call doc.Removefromfolder("($inbox)")
and change
Set doc = .GetNextDocument(doc)
to
set doc = nextDoc
However, your code with the putInFolder and RemoveFromFolder calls is not actually inside the loop, therefore only the first doc that you process will be moved to the Processed folder and nextDoc will not be properly set after the first iteration of the loop. If you really only want to move the first document to the Processed folder, then the above solution still isn't right because you'll only be setting nextDoc once, outside the loop and you will have an infinite loop since you'll always be setting doc to the same nextDoc value. You'll need another instance of nextDoc = getNextDocument(doc)
inside the loop. If you really want all documents to be moved to the Processed folder, then you just need to move the entire block of code dealing with the folders and assigning nextDoc into the inside of the loop.
Upvotes: 1
Reputation: 12080
The problem is simple: The document is removed from Inbox and therefor is not in the index anymore. This happens if one of two conditions are true:
Despite of the fact, that in your code there is a lot of "nonsense" (sorry to say), it is easy to fix this code:
Just use the fact, that the view- object can be updated as advantage and change to "getfirstdocument" all the time:
Change this line:
Set doc = .GetNextDocument(doc)
to these lines:
Call .Refresh()
Set doc = .GetFirstDocument
What does this do: The Refresh
removes the currently processed document from the view. The "next" document will be the first in view. And that one you get until there is no more "first" document...
And: Richard is right. You need to move the two rows Call doc.PutInFolder("Processed")
Call doc.Removefromfolder("($inbox)")
below the Do
in order to move ALL documents to the folder and not only the first one.
Upvotes: 1