This tip will perform the following functions: -select one or more documents (same form) - agent gets all the fields in the form and presents them alphabetically in an inputbox - choose a field - enter the new value of that field - agent changes the fieldvalue for all selected documents
Only a few errors are anticipated. In most cases when an error occurs the agent stops. It is imperative that you know what you are doing, otherwise this agent can do a lot of damage.
Sub main
Dim session As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim form As Notesform
Dim formvariant As Variant
Dim FieldCounter As Integer
Set db=session.CurrentDatabase
Set dc=db.UnprocessedDocuments 'select all selected documents
FieldCounter = 0
If dc.count=0 Then ' no documents selected
Msgbox "No documents selected" 'display error message
Exit Sub ' end script
Else 'at least 1 document has been selected
Set doc = dc.GetFirstDocument ' get first document from
NotesDocumentCollection
formvariant = doc.form(0) 'get the name of the form
Set form = db.GetForm(formvariant) ' set form
End If
Dim itemType As Integer ' 768=numeric , 1024=datetime ,
1280=text
Dim item As NotesItem
Dim Veldtype As String
Forall field In form.Fields 'get all fields in the form
FieldCounter = FieldCounter + 1
Redim Preserve FieldGroup(FieldCounter-1) 'keep the values
in the array
Set item = doc.GetFirstItem( Field )
itemType = item.Type ' 768=numeric , 1024=datetime ,
1280=text
Select Case itemType
Case 768 : Veldtype="Num"
Case 1024 : Veldtype="Dat"
Case 1280 : Veldtype="Txt"
Case Else : Veldtype="???"
End Select
FieldGroup(FieldCounter-1) = Field + " " + Veldtype '
example "FIELDNAME Txt"
End Forall
' sort the array alphabetically
Dim swapped As Integer
Dim LoopCounter As Integer
Dim temp As Variant
Do
swapped = 0
For LoopCounter = 0 To FieldCounter-2
If FieldGroup(LoopCounter + 1) <
FieldGroup(LoopCounter) Then ' if yes then swap
temp = FieldGroup(LoopCounter) 'swap
FieldGroup(LoopCounter) =
FieldGroup(LoopCounter + 1) 'swap
FieldGroup(LoopCounter + 1) = temp 'swap
swapped = 1
End If
Next LoopCounter
Loop Until swapped = 0 'all are sorted
' end sort array alphabetically
Dim workspace As New notesuiworkspace
Dim ChosenField As String
Dim ChosenFieldLength As Integer
ChosenField=Cstr(workspace.Prompt(PROMPT_OKCANCELLIST,"Keuze","Te
xt","",FieldGroup())) 'prompt for fields
ChosenFieldLength=Len(ChosenField) ' determine length of the
chosen field
Dim ChosenFieldCut As String
If Right$(ChosenField,3)="Num" Then 'Numeric
ChosenFieldCut = Left$(ChosenField , ChosenFieldLength - 6)
'FIELDNAME minus " Num"
NewValue = Clng(Inputbox("Enter the new value of " +
ChosenFieldCut)) ' set new value of the chosen field
End If
If Right$(ChosenField,3)="Txt" Then 'Text
ChosenFieldCut = Left$(ChosenField , ChosenFieldLength - 6)
'FIELDNAME minus " Txt"
NewValue = Cstr(Inputbox("Enter the new value of " +
ChosenFieldCut)) ' set new value of the chosen field
End If
For LoopCounter = 1 To dc.count 'for all selected documents
Set doc = dc.GetNthDocument(LoopCounter) 'get document x
doc.ReplaceItemValue ChosenFieldCut , NewValue 'replace the
value of the field that has been chosen
Call doc.save(True,True) 'save the document
Set doc = dc.GetNextDocument(doc) ' get the next document
Next
End Sub
This was first published in June 2001