Inserting RTF Text at a specific location

  • I have a situation nearly identical to the one reported by "computech" on July 21, 2009. I have paragraphs of RTF Text stored in tables that I need to merge with my main RTF document.

    I know from that prior post I have the option of using LoadFromStream or SelectionAsString to insert my text. Since I need to keep the formatting, I plan to use LoadFromStream.

    I under I can set CPPosition to indicate where the text should be inserted. This is where my problem comes in. I don't know how to determine the correct location for insertion. The location where the RTF paragraphs need to go is set by the user when they define a document template - essentially, they create a merge field in the document and point it at a table containing RTF paragraphs.

    Thus, it is the location of this merge field where I need to insert the RTF paragraph. Originally, I tried using your Finder object to locate my RTF paragraph merge field but this did not work for me. Then I tried using the OnMailMergeGetText event. This allows me to trap my RTF paragraph tags but I cannot see how to determine their location. Or is the CPPosition value automatically set when a merge tag is found?

    Also, assuming I can determine the proper location to insert my RTF paragraph, I need for the inserted paragraph to replace the existing mailmerge tab. What is the best way to hide the tag once the merge occurs?

    • Offizieller Beitrag

    Hi,

    usually the merge fields are used to store the new text within. This is done in OnMailMergeGetText.

    Since WPTools 5 the cursoir actually is moved tot the location of the field, to the first character inside. So You CAN use LoadFromStream inside the OnMailMergeGetText event.

    You can also use

    function TWPCustomRtfEdit.MoveToField(const fieldname: string; from_start: Boolean = TRUE): Boolean;

    to locate the field and insert the data.

    It insert the data after the field go to the end object.

    This can be done with this code:

    Code
    if WPRichText1.MoveToField('FIELD1') then
           WPRichText1.TextCursor.ObjBeforeCP :=
              WPRichText1.TextCursor.ObjAtCP.EndTag;
        WPRichText1.InputString('<--');
  • Just a couple of quick observations about my issue. First of all, it finally works - thank you for your help Julian.

    However, unless I was just doing something drastically wrong (which I will freely admit is very possible...) I found the following:

      1. The TWPRichText.LoadFromStream did NOT correctly read in my text from a stream (the beginning of it was always truncated); additionally, the name of my merge field remained displayed in the document.

      2. Within the OnMailMergeGetText event, the Contents property has a LoadTextFromStream method. This DID work correctly. It replaced the merge tag with the RTF content that I needed to merge.

      3. I think I misunderstood part of your suggestion about the use of:

    Code
    WPRichText1.TextCursor.ObjBeforeCP := 
       WPRichText1.TextCursor.ObjAtCP.EndTag


    Although I inserted this code in my OnMailMergeGetText event and verified I had a valid merge field, ObjAtCP was ALWAYS nil and resulted in an A/V error every time.


  • But as I said, what I needed to accomplish works so that is okay with me. I am just reporting the above in case I found a bug.

    Thanks for your help.

    • Offizieller Beitrag

    Hi,

    Zitat

    But as I said, what I needed to accomplish works so that is okay with me. I am just reporting the above in case I found a bug.

    I assume you use mail merge as it was intended, good that it works.

    The code:

    WPRichText1.TextCursor.ObjBeforeCP :=
    WPRichText1.TextCursor.ObjAtCP.EndTag;

    was intended to be used after MoveToField. Inside the OnMailMergeGetText event the cursor must not be moved.

    Regards,
    Julian