Cycling through $Body form fields in LotusScript

Hey All;
I'm a fairly advanced VB/VBA coder trying to write LotusScript to cycle through all the forms within a NSF database and read through each one's $Body field (or fields if there's more than one) to look for specific strings.  I've tried several different methods such as doc.FieldGetText( "$Body" ), but I only get errors like "Type Mismatch".  The online help file says "A Field object has no properties or methods" such as Field.Value; how can I read this value in code?

Thanks very much in advance!
Who is Participating?

Improve company productivity with a Business Account.Sign Up

HemanthaKumarConnect With a Mentor Commented:
Treat it as NotesDocument.. NotesForm will hide internal fields
$Body is a richtext field and it sometimes could have been signed .

Treat it as richtext and use GetFormattedText method to extract text values.

evets27Author Commented:
Just to clarify too; I'm working with R5 not R6
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Will do.
Echoing Hemantha... but you also need to use a different object type.  Since you are using FieldGetText, you must be using a NotesUiDocument object, which is merely the "front end" -- an on-screen document DISPLAYED USING the form, not the form's design itself.  You want the form itself, which in R5, you can only treat as a NotesDocument and "go hunting" for it.  Otherwise, you have to resort to the C-level API.
evets27Author Commented:
Thanks for the input guys; I see I have a little to learn about Lotus!  I'm not sure what you mean exactly by 'going hunting'; are there specific functions I can use to do this?  For what I'm looking at I don't think I can invest the time in working with a C-level API; there really isn't an easy way, with the Lotus object model, to loop through and read these values??  When I view the Design Properties for the form it's all right there.  As far as 'front end' vs. back end, I don't really care HOW I get the results, as long as I can read through the $Body text and work with it in code.

Thanks again for responding!  Sorry if I'm being difficult here...
In R5, there is no explicit mechanism for finding design objects.  If you go to the Sandbox, there is a tool called DBDesign that uses LotusScript to declare the external DLL functions for finding the deign elements.  Using VB, you could do the same.

Once you have the BACK END (NotesDocument) object, you can retrieve the NotesRichTextItem named $Body from it using NotesDocument.getFirstItem.  You can then get the text of the rich text $Body field via getFormattedText or via .Text.  Don't expect to be able to make changes to it, though.
qwaleteeConnect With a Mentor Commented:
Note: all "duplicate" $Body fields are treated as if they are a single field in LotusScript.  In early versions of the object model, one used .getFirstItem and .getNextItem to cycle theough them.  As of, I think, R4.11, .getNextItem was deprecated to always return Nothing, and the NotesItem object was changed to that for rich text items, multiple items are treated as one.  (Unfortunately, it is possible for other types to have duplicate items, and there is no easy way to get to them.)
evets27Author Commented:
Sorry to be high-maintenance about this (I'll raise the point value of this question); but all of the documentation I'm seeing says "'...set value of doc..." without giving good examples of how to do it.  I've tried looping through the Forms collection using "Forall form In db.Forms" and that works all right; then I loop through the Fields collection to get the names of the fields.  But I can't get the values from there.  Can I get to the $Body value via the Forms collection or will I have to open it as a NotesDocument, then use getFirstItem > getFormattedText?  And I won't need to make form changes, just browsing through so if $Body is read-only that's no problem.
If you have access to a Notes Designer, than this DB might be of help for you:

pgloorConnect With a Mentor Commented:
The following LotusScript code basically does what you want.  It uses some API calls which are required to get access to the design note.

I cannot provide a VB example because due to a system crash (don't worry, not related to this code) I have currently no access to it.

' DesignFormProcessing:

Option Public
Option Declare ' Use Option Explicit in VB


' In this example only NOTE_CLASS_FORM will be needed
Const NOTE_CLASS_FORM% = &H0004  '*** form note
Const NOTE_CLASS_VIEW% = &H0008  '*** view note
Const NOTE_CLASS_FILTER% = &H0200  '*** filter note
Const NOTE_CLASS_FIELD% = &H0400  '*** field note
Const NOTE_CLASS_REPLFORMULA% = &H0800  '*** replication formula
Const NOTE_CLASS_ALL% = &H7FFF '*** all note types

  nid As Long             ' typedef DWORD NOTEID;
End Type

Declare Function W32_NSFDbOpen Lib "nnotes.dll" Alias "NSFDbOpen" _
( Byval dbName As String, hdb As Long ) As Integer
Declare Function W32_NSFDbClose Lib "nnotes.dll" Alias "NSFDbClose" _
( Byval hdb As Long ) As Integer

 char far *Name,
 WORD  Class,
 NOTEID far *retNoteID);
Declare Function W32_NIFFindDesignNote _
Lib "nnotes.dll" _
Alias "NIFFindDesignNote" ( _
Byval hdb As Long, _
Byval deName As String, _
Byval declass As Integer, _
retNoteID As NOTEID ) As Integer

Sub Initialize
  ' Notes Objects
  Dim session As New NotesSession
  Dim db As NotesDatabase
  Dim dc As NotesDocumentCollection
  Dim doc As NotesDocument
  Dim note As NotesDocument
  Dim rtItem As NotesRichTextItem
  ' API related variables
  Dim hDb As Long
  Dim rc As Long
  Dim nid As NOTEID
  ' Open the database and get its API handle
  Set db = session.CurrentDatabase
  ' Check if there are any forms in this database
  If Isempty(db.Forms) Then
    Print "No Forms available"
    Exit Sub
  End If
  ' Get an API handle to the database
  If db.Server = "" Then
    ' No Server specified, just use filepath
    rc = W32_NSFDbOpen( db.Filepath, hDb )
    ' Open database on specified server using
    ' a netpath construct (db!!filepath)
    rc = W32_NSFDbOpen( db.server & "!!" & db.Filepath, hDb )
  End If
  ' Check the return code
  If rc <> 0 Then
    ' Return code must be zero, otherwise an error occurred
    Print "Couldn't open db  [" & Cstr(rc) & "]"
    rc = W32_NSFDbClose( hDb )
    Exit Sub
  End If
  ' Iterate thru the list of forms and get each forms design note
  Forall forms In db.Forms
    ' Get the form design note using the form name
    rc = W32_NIFFindDesignNote( hDb, forms.Name, NOTE_CLASS_FORM%, nid )
    ' Check for API errors
    If rc <> 0 Then
      ' Return code must be zero, otherwise an error occurred
      Print "Couldn't get design note [" & Cstr(rc) & "]"
      rc = W32_NSFDbClose( hDb )
      Exit Sub
    End If
    ' Get the design note as a notes document for
    ' further processing with LotusScript
    Set note = db.GetDocumentById(Hex(nid.nid))
    ' Always make sure a document has been returned
    If Not note Is Nothing Then
      ' Get the $Body item
      Set rtItem = note.GetFirstItem("$Body")
      ' Process the body item here
      Print rtItem.GetFormattedText( True, 0)
    End If
  End Forall  
  ' Close the database (API)
  rc = W32_NSFDbClose( hDb )
End Sub

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.