​A selection of topics on IT and its application to finance. 
​Send me your comments, questions ​or suggestions by clicking

Reading a lotus notes mail folder with VB6

I had a problem recently where I needed to, on a daily basis, automatically process an email sent to us by a customer containing a particular subjectline. The processing involved was to simply read the subject line and, depending on whether or not it contained the required text, save the file attachment to my PC. So far so easy, but the problem was that I didn't have access to LotusScript and the server dis-allowed the running of scheduled agents of any sort and I couldnt figure out another way to do it.   After reading some of the Notes Designer on-line help though it quickly became apparent that I should be able to process the email - in fact all my emails - via Visual Basic and COM. The code below shows one way in which it could be done.To use it you first have to include the Domino object library into your Visual Basic project:  

Choose Project -> References.  

Check "Lotus Domino Objects".

' A VB program to save a Lotus Notes email attachment to your PC.

Sub Main ()  

Dim s As New NotesSession
Dim db As NotesDatabase
Dim v As NotesView
Dim vn As NotesViewNavigator
Dim e As NotesViewEntry
Dim doc As NotesDocument
Dim rtitem As Variant
Dim nit As NotesItem  
On Error GoTo err_handler

Call s.Initialize("your_notes_password")  
Set db = s.GetDatabase("your_server_name", "your_nsf_filename")  
Set v = db.GetView("($Inbox)")  
Set vn = v.CreateViewNav()  
Set e = vn.GetFirstDocument()  

Do While Not (e Is Nothing)  
' for each document in my inbox
     Set doc = e.Document  
     Set nit = doc.GetFirstItem("subject")    
' Only procees the email if it contain the
' particular subject line we're interested in '
     pos = InStr(1, nit.Values(0),"Testing Testing 123")
     If pos = 1 Then  
          FileName = "C:\myfile.txt"  
          Set rtitem = doc.GetFirstItem("$file")  
          For Each Z In rtitem.Values  
               Set object = doc.GetAttachment(Z) object.ExtractFile (FileName)  
' In my case I know that the email will only ever contain
' one attachment so I can exit after one pass
' through the loop but you could extract multiple
' attachments if your email contained them '
          Exit For  
' Copy the processed email to another folder so
' we don't accidentally process it again
' doc.PutInFolder "Processed", True  
' Now we can safely remove it from our inbox  
          doc.RemoveFromFolder "($Inbox)"
' I'm only interested in the first document with
' the relevant subject line so I can exit
' after processing it
          Exit Do
     End If
     Set e = vn.GetNextDocument(e)  
' Let me know everything is OK '
retval = Shell("cmd /c net send tom data file detached OK", vbHide)  
Exit Sub    

info = "A data file detach error has been detected." & _
"Message is: " & Err.Description
commandstr = "cmd /c net send " retval = Shell(commandstr & "tom " & info, vbHide)  

Resume endit  

End Sub