Find and Replace for All Items in a Group of Documents

Mindwatering Incorporated

Author: Tripp W Black

Created: 06/11/2002 at 08:59 AM

 

Category:
Notes Developer Tips
Agents, LotusScript

Sub Initialize
' this agent will change the domain information on a person's hierarchical name - e.g. "/ACME/US"
Dim s As New NotesSession
Dim db As NotesDatabase
Dim agent As NotesAgent
Dim col As NotesDocumentCollection
Dim doc As NotesDocument
' Dim item As NotesItem
Dim stringtoreplace As String
Dim stringtoreplacechars As Integer
Dim replacementstring As String

Dim newvalues() As String ' Array List of values of an item and its replacements
Dim newvalue As String ' current value being transferred or replaced

Dim count As Integer ' used as a flag and also as a array/field value index
Dim anitemhaschanged As Integer ' used as a flag to show that an item has changed and that the doc needs to be saved.

' _________________________________

stringtoreplace = "oldstringvaluetofindgoeshere"
replacementstring = "newreplacementstringvaluegoeshere"


' _________________________________

stringtoreplacechars = Len(stringtoreplace) -1
Set db=s.CurrentDatabase
Set agent = s.CurrentAgent
Set col = db.UnprocessedDocuments
Set doc = col.GetFirstDocument
While Not (doc Is Nothing)
' check the items in the doc

Forall item In doc.Items
count=0
anitemhaschanged=0
If Not (item Is Nothing) Then
Forall v In item.Values
' Messagebox( v )
' update item --> check and see look for and replace search string
newvalue=v
Do While Instr(newvalue, stringtoreplace) > 0
' scan for replacement
newvalue = Left$(newvalue, Instr(newvalue,stringtoreplace)-1) + replacementstring + Right$(newvalue,Len(newvalue)-(Instr(newvalue, stringtoreplace) + stringtoreplacechars))
Loop
' redim array for new size
Redim Preserve newvalues(count)
newvalues(count) = newvalue
count = count+1
End Forall
If count>0 Then
' if the item has been changed then update the item's value(s)
Set item = doc.ReplaceItemValue( item.Name, newvalues)
anitemhaschanged =1
End If
' reset variables for next field
count=0
Redim newvalues(0)
End If
End Forall
' save the doc
Call doc.Save(True, False)
' cycle to next doc
anitemhaschanged=0
Set doc = col.GetNextDocument(doc)
Wend

End Sub

previous page