lotus-noteslotus-dominolotusscriptagentlotus

How to create a document and then replace the values of the fields in the specific document


I have an agent which imports records from Excel to Notes. At the moment each time, it runs it creates a new document. I would like it:

  1. The first time it runs to create the document.
  2. The next time it will run, to replace the field values of the specific document NOT to create a new one. How can I fix my agent which is:

Sub Initialize

Dim session As New NotesSession 
Dim db As NotesDatabase 
Dim doc As NotesDocument 
Dim xlApp As Variant, xlsheet As Variant, xlwb As Variant, xlrange As Variant 
Dim filename As String, currentvalue As String 
Dim batchRows As Integer, batchColumns As Integer, totalColumns As Integer 
Dim x As Integer, y As Integer, startrow As Integer 
Dim curRow As Long, timer1 As Long, timer2 As Long 
Dim DataArray, fieldNames, hasData 
Dim view As NotesView

Set db = session.CurrentDatabase
Set view = db.GetView("test-forecast")
Set doc = view.GetFirstDocument

timer1=Timer 
filename="C:\DM\Forecast\forecast-a.xlsx" 
batchRows=2 'process 2 rows at a time 

Set db=session.CurrentDatabase 
Set xlApp = CreateObject("Excel.Application") 
xlApp.Visible = True 'set Excel program to run in foreground to see what is happening 
Set xlwb=xlApp.Workbooks.Open(filename) 
Set xlsheet =xlwb.Worksheets(1) 

Redim fieldNames(1 To 5) As String 

DataArray=xlsheet.Range("A1").Resize(batchRows, 5).Value 'get worksheet area of specified size 

For y=1 To 5 'we assume max 5 columns in the sheet 
    currentvalue=Cstr(DataArray(1,y)) 
    If currentvalue<>"" Then 'abort counting on empty column 
        fieldNames(y)=currentvalue 'collect field names from the first row 
        totalColumns=y 
    Else 
        y=2 
    End If 
Next 

Redim Preserve fieldNames(1 To totalColumns) As String 
    
curRow=2
hasData=True 
While hasData=True 'loop until we get to the end of Excel rows 
    If curRow=2 Then startrow=2 Else startrow=1 
    For x=startrow To batchRows 
        curRow=curRow+1 
        If Cstr(DataArray(x,1))+Cstr(DataArray(x,2))<>"" Then 'when 2 first columns are empty, we assume that it's the end of data 
            Print Cstr(curRow-2) 
            Set doc=New NotesDocument(db)
            doc.Form="test-forecast"
            doc.Type="test-forecast"
            For y=1 To totalColumns 
                currentvalue=Cstr(DataArray(x,y)) 
                Call doc.ReplaceItemValue(fieldNames(y), currentvalue) 
            Next 
            Call doc.save(True, False) 
        Else 
            hasData=False 
            x=batchRows 
        End If 
    Next 
    If hasData=True Then DataArray=xlsheet.Range("A"+Cstr(curRow)).Resize(batchRows, totalColumns).Value 'get worksheet area 
Wend 
timer2=Timer 
Call xlApp.Quit() 'close Excel program 

End Sub

Thank you in advance.


Solution

  • As of your comment you only have 2 rows in your excel- file: The first row contains the fieldnames, the second row contains the values.

    Every import only contains one document. And you want to update this single document on every run.

    There are multiple ways to get that document, the fastest is by using a view containing that document. Create a view with Selection formula: SELECT Form = "test-forecast". Give it a speaking name like (ViwLkpDocument). If you really have only one import with one document, then you can keep the default column, otherwise you might sort the first column by some sort of key to identify the matching document.

    I would add a function for that so that you can change the method later if your requirments change:

    Function GetDocument(db as NotesDatabase) As NotesDocument
      Dim viwLkp as NotesView
      Dim docTmp as NotesDocument
      Set viwLkp = db.GetView( "(ViwLkpDocument)" )
      Set docTmp = viwLkp.GetFirstDocument
      If docTmp is Nothing then
        Set docTmp = New NotesDocument( db )
        docTmp.Form="test-forecast"
        docTmp.Type="test-forecast"
      End If
      Set GetDocument = docTmp
    End Function
    

    Then change your code like that:

    ...
    Print Cstr(curRow-2) 
    Set doc=GetDocument(db)
    For y=1 To totalColumns
    ...