Richtext find and replace

Posted on 2006-06-01
Last Modified: 2013-12-18
Hi Guys
Anybody has a quick code to find and replace a text in richtext using NotesDXL . If yes, can it be shared :). I had to do some stuff quickly and i don't want the headache of writing the entire code. Any ideas, and oh i know about Midas, but i specifically need NotesDXL and NotesDomParser code.

Question by:p_partha
    LVL 46

    Expert Comment

    by:Sjef Bosman
    Hi Bro, yours is a difficult question... I'd be happy to develop something with you, but since you asked for a quicky I didn't respond. You suggest that, to write the code, you'll end up with a headache, but is it really that difficult? Let's dive into it!

    First, let's have some design. How id you think to attack this interesting problem? Something like

        get document
        get richtext field
        dump richtext to file using Notes' DXL methods
        parse DXL and make alterations
        reload richtext from DXL
        save document
    LVL 14

    Author Comment

    Bro, if you are with me on this, we can easily crack it  .....

    But here is some code i got from, but couldn't make it to work, as it was throwing a error that Domparser is not initialized, i must be doing something silly , dont' know what , may be another pair of eyes would do :

          Dim session As New notessession
          Dim db As notesdatabase
          Set db = session.currentdatabase
          Dim ws As New notesuiworkspace
          Dim uidoc As notesuidocument
          Dim doc As notesdocument
          Set uidoc = ws.currentdocument
          Set doc = uidoc.document
          Dim exporter As NotesDXLExporter ' exports notes doc to XML code
          Dim domParser As NotesDOMParser ' transforms XML code to a DOM tree
          Dim importer As NotesDXLImporter ' imports XML code as notes document
          Dim itemList As NotesDOMNodeList ' a list of <item>-nodes
          Dim node As NotesDOMNode 'an <item> node
          Dim aNode As NotesDOMAttributeNode 'a "name" attribute
    'create a temporary RichText field in the notes doc you want to work with
          Set rt = doc.createRichTextItem("TMPXML")
          Set exporter = session.CreateDXLExporter
          Set domParser = session.createDOMParser
          Set importer = session.CreateDXLImporter
    'set the NotesDocument as input for the exporter:
          Call exporter.SetInput(doc)
    'set the exporter as input and the rt as output for the domParser:
          Call domParser.setInput(exporter)
          Call domParser.SetOutput(rt)
    'set some options for the importer:
          importer.ACLImportOption = DXLIMPORTOPTION_IGNORE
          importer.DesignImportOption = DXLIMPORTOPTION_IGNORE
          importer.DocumentImportOption = DXLIMPORTOPTION_REPLACE
    'run the exporter:
          Call exporter.process
    'get the list of <item> elements:
          Set docNode = domParser.Document
          Set rootElement = domParser.Document.DocumentElement
          Set itemList = rootElement.GetElementsByTagName ("item")
    'and search the itemList for your RichText field, f.g. "Body":
          For k = 1 To itemList.NumberOfEntries
                Set node = itemList.GetItem(k)
                Set aNode = node.Attributes.GetItem(1)
                If (Ucase(aNode.AttributeValue) = Ucase("BLASTEMAILBODY")) Then
    ' here we have the node which represents the BODY-field of the notes document
    ' and we're delegating the work to a sub-function
                      Call parseChild(node.FirstChild, doc)
                End If
          Next '<item> node
    ' the parseChild() has done all work, now we have a modifed tree of XML data in the
    ' domParser object
    ' now we order the domParser to put out his data to the temporary RichText field
          Call domParser.serialize()
    ' and set this RichText field as input for the importer
          Call importer.setInput(rt)
    ' the output is for sure this current database
          Call importer.setOutput(db)
    ' and tell the importer to do the import-process
          Call importer.process()
    ' finally we're removing the temporary RichText field
          Call doc.RemoveItem("TMPXML")
    'the sub "parseChild" does the work (and does some recursion to walk down the DOM tree):
    LVL 14

    Author Comment

    Oh, here is other functions :
    Private Sub parseChild(node As NotesDOMNode, doc As NotesDocument)
          Dim child As notesdomnode
          Dim numChildNodes As Integer
          If Not (node.isNull) Then
                numChildNodes = node.NumberOfChildNodes
                If Not ( node.HasChildNodes) And (node.NodeType = DOMNODETYPE_TEXT_NODE) Then
                      Call doWork(node, doc)
                End If
                Set child = node.FirstChild
                While numChildNodes > 0
                      Call parseChild(child, doc)
                      Set child = child.NextSibling ' Get next node
                      numChildNodes = numChildNodes - 1
          End If
    End Sub

    Private Sub doWork(node As NotesDOMTextNode, doc As NotesDocument)
          Dim s As String
          s = node.nodeValue
          l = Len(s)
          s = "Partha"
          Call node.replaceData(1, l, s)
    End Sub
    LVL 46

    Expert Comment

    by:Sjef Bosman
    I left my eyes on the couch downstairs... Maybe later today. ;-)
    LVL 46

    Expert Comment

    by:Sjef Bosman
    Ah, oh, no, can't be true... Don't you use
        Option Declare
    in your Options section?? Otherwise you would have found out that DXLIMPORTOPTION_REPLACE has no value. Unless of course your code above isn't complete, and there is an include-file missing. The same probaly goes for the other constants. Ah, no, it's the only one: it just doesn't exist, it's either DXLIMPORTOPTION_REPLACE_ELSE_IGNORE or DXLIMPORTOPTION_REPLACE_ELSE_CREATE

    Tsk, tsk, tsk!
    LVL 46

    Expert Comment

    by:Sjef Bosman
    It stops on the line
        Call domParser.SetOutput(rt)
    indeed with the error you mentioned, but there's more:
        DomParser output object is invalid, uninitialized, or not explicitly declared and strongly typed.

    I assume therefore that you must do something with rt. What? That will be discussed in the episode after my dinner ;)
    LVL 14

    Author Comment

    I did declare rt and it replaces kind of now... which is a good sign:) i will keep you posted abt the other learnings. This process has taken most of my time (although i don't have any :(( )

    LVL 46

    Accepted Solution

    I tested with one of the test databases, and indeed it "replaces kind of". The big danger of the Importer is that it overwrites existing documents, so you might take some precautions. What I'd do:
    - copy document
    - change form name, so it won't appear anywhere
    - save that copy
    - put the TMP field in yet another temp document
    - export, process and import the copy
    - copy the rich text field back to the original document
    - scrap anything you don't need

    Be careful with the copy document, users may not have Delete permission
    LVL 14

    Author Comment

    Thanks Bro
    LVL 46

    Expert Comment

    by:Sjef Bosman
    So it works (kind of)... ;)

    Thanks for the grade, and you're always welcome!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    For Desktop Techs: How to retain a user's Notes configuration data when swapping out the end user's computer. (Assuming that you are not upgrading to a completely different version of Notes client) All you need to do is: 1) install Notes o…
      In today’s Arena we can’t imagine our lives without Internet as we are highly used to of it. If we consider our life style just for only 2 min we found that face to face communication is swapped by e-communication.  Every Where from Works place to…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now