LotusScript auto attaching files

Hello,

I have four to five hundred records in a very simple database (One Form and One View).  

Every single record has a serial no. as part of the record.  Ther serial number is in the format 73147238 with the TYPE set as Number.  The type can be changed if necessary.

I also have a CD with four to five hundred subdirectories, each subdirectory name relates to serial number.  I.e. A record in Notes has the serial no. 73147238 and their is a directory called D:\73147238

This directory contains two small files Prima.lib and Serialid.1 with a combined size of 440 Bytes.  Their are only the two files in the directory and nothing else.

I need an Agent\Script etc. which will look at the Serial No. of the record and attach the corresponding files from the subdirectory.  

This is only a one off thing and I'll have the database on my local machine and the CD in my local machine.

Hope thats enough info, look forward to some suggestions about how best to go about this, I have zero LotusScript skills and a reasonable amount of Notes Database writing skills.

Thank you

Rhomulos
LVL 2
rhomulosAsked:
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.

nimrodoCommented:
Try looking in the NotesRichTextItem.EmbedObject method for putting attachments in your "Body" rich-text field.
0
ArunkumarCommented:
Will get back to you shortly with the code.

:)
0
dalehendCommented:
You did not say how the serial number is in the Document, but if  it is in a Title box(field) or something then you can retrieve it via
doc.Title or whatever.
For example,

Dim session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim fileName As String
Dim rtItem As NotesRichTextItem

fileName = Dir(doc.Title + "\" "Prima.lib" , 15)
Set rtitem = New NotesRichTextItem( doc, "Attachment")
Call rtitem.EmbedObject(EMBED_ATTACHMENT, "", fileName)

 
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

ArunkumarCommented:
Hi Rhomolos !

Sorry for the delay.  I had some work here.  Anyway, Here is the script you can put in an agent.

The Variables have to be filled in by you.  Thats it.  This is tested OK !

 Dim ss As New notessession
     Dim db As notesdatabase
     Dim view As notesview    
     Dim doc As notesdocument
     Dim rtitem As notesRichTextitem
     Dim object1 As NotesEmbeddedObject    
     Dim object2 As NotesEmbeddedObject    
     Dim variable As String
     Dim filepath As String
     
     Set db =  ss.currentdatabase
     Set view = db.GetView("<<<<<Your View Name>>>>>")
     
    Set doc = view.GetFirstDocument
     
     Do While Not (Doc Is Nothing)
         
          variable = doc.FieldNameThatContainsTheSerialNumber
         
          Set rtitem = doc.GetFirstITem("<<<FieldThatGetsTheFileAttached>>>")  ' If you have already a rich text field
         
          If rtitem Is Nothing Then ' If you do not have a richtext field
               
               Set rtitem = New NotesRichTextItem( Doc , "TheNameOfTheRichTextFieldYouWantToHave" )
               
          End If
         
         
          filepath = "D:\" & variable & "\"
         
          Set object = rtitem.EmbedObject( EMBED_ATTACHMENT, "",  filepath & "Prima.lib" , "Prima.lib" )
          Set object2 = rtitem.EmbedObject( EMBED_ATTACHMENT, "",  filepath & "serialid.1" , "serialid.1" )    
         
          Call Doc.Save(True,False)
         
          Set doc = view.GetNextDocument(doc)
     Loop




Good Luck !

-Arun
0
rhomulosAuthor Commented:
Hello,

Code looks great, unfortunately their must be somethine funny about my notes or enviroment.  I'm using NOtes 4.6.3b and the database is on local not running on a server.  Dunno if that makes any difference.

I'm unable to use the line
Dim ss As New Notessession
as it comes up in red.

Dim SS As notesession
does work however but when I run the agent I get an hour glass for a second or two and then it returns control to me, no attachments in the documents and no error message or any indication of anything actually have happened :-(

Here is the code I used after I put in the variables, etc.

In the Declarations section

Dim ss As  notessession
Dim db As notesdatabase
Dim view As notesview      
Dim doc As notesdocument
Dim rtitem As notesRichTextitem
Dim object1 As NotesEmbeddedObject      
Dim object2 As NotesEmbeddedObject      
Dim variable As String
Dim filepath As String


In the Sub MAIN section (Does the Subroutine MAIN run automatically?)
Sub MAIN
     Set db =  ss.currentdatabase
     Set view = db.GetView("Main View")
     
     Set doc = view.GetFirstDocument
     
     Do While Not (Doc Is Nothing)
         
          variable = doc.SerialNo  'The field serialno is of the type text
         
          Set rtitem = doc.GetFirstITem("Attachments")  'Attachments is a empty Rich Text Field
         
          filepath = "D:\" & variable & "\"  'On my D:\ drive I have the subdirectory with the same name as the SerialNo field
         
          Set object = rtitem.EmbedObject( EMBED_ATTACHMENT,"",  filepath & "Prima.lib" , "Prima.lib" )     'Prima.lib exists in the directory
          Set object2 = rtitem.EmbedObject( EMBED_ATTACHMENT,"",  filepath & "serialid.1" , "serialid.1" )     'Serialid.1 exists in the directory
         
          Call Doc.Save(True,False)
         
          Set doc = view.GetNextDocument(doc)
     Loop
End Sub

I daresay I've made a silly newbie mistake,  look forward to a nudge in the right direction.  Increased points too.

Regards

Rhomulos
0
nimrodoCommented:
You can't declare "new NotesSession" in the (Declarations) part. Either put it in your Main function or set it by using

set session=new NotesSession

in your function.
0
ArunkumarCommented:
Hi Rhomulos,

Copy the entire code in the INITIALISE event of the Agent.  

Dont split it into declarations and SUB and all other crap.

Good Luck !

--Arun

PS:  Are you using this script along with anyother script ?
0
AntonysinghCommented:
Hi rhomulos,

Use this modified verson of the code.

Place the following code in declaration part . (This already you have)

Dim ss As  notessession
Dim db As notesdatabase
Dim view As notesview      
Dim doc As notesdocument
Dim rtitem As notesRichTextitem
Dim object1 As NotesEmbeddedObject      
Dim object2 As NotesEmbeddedObject      
Dim variable As String
Dim filepath As String


In your agent's initialize event place this code

Call MAIN ' This will call the function MAIN

In the function main place the following code.

Sub MAIN
      Set ss = New notessession ' You have to set notessession here
      Set db =  ss.currentdatabase
      Set view = db.GetView("Main View")
      Set doc = view.GetFirstDocument
      
      Do While Not (Doc Is Nothing)
      
      variable = doc.SerialNo(0)  'The field serialno is of the type text
      'variable = "Download"
      Set rtitem = doc.GetFirstITem("Attachments")  'Attachments   is a empty Rich Text Field
      
      filepath = "c:\" & variable & "\"  'On my D:\ drive I have the subdirectory with the same name as the SerialNo field
      
      
      Set object = rtitem.EmbedObject(EMBED_ATTACHMENT,"",  filepath & "Prima.lib" , "Prima.lib")     'Prima.lib exists in the directory
      Set object2 = rtitem.EmbedObject(EMBED_ATTACHMENT,"",  filepath & "serialid.1" , "serialid.1" )     'Serialid.1 exists in the directory
      
      Call Doc.Save(True,False)
      
      Set doc = view.GetNextDocument(doc)
Loop
End Sub

I have modified and tested this code its works fine.

Good luck.

Regards,
Antony.


0

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
AntonysinghCommented:
Hi

please note

filepath = "c:\" & variable & "\"  'On my D:\ drive I have the
                      subdirectory with the same name as the SerialNo field


I have changed to "C" drive , you change to D or whereever you want.

-Antony.
0
rhomulosAuthor Commented:
Hello,

I placed all the code straight into Initializa seciotn, I am running no other scripts or code in conjunction with this.

I had to change my serialno field to RichText as I was getting Type Mismatch errors but that is not a problem.  I'm only doing a one off run to attach these documents so I can change it back afterwards.

Apart from that it works grate, excellent piece of code.

what does the (0) in
variable = doc.SerialNo(0)
signify though??

I added the line
on error resume next
to take care of the problem of hte directory not always existing on the harddrive, this seemed to work fine.

Once again thanks for the assistance, a consulting company quoted me two days work at $110 p/hr to do this exact same task.

Regards

Rhomulos
0
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.