Copying the whole contents of one WPRTF to another

  • Sorry for being so dense. I have pushed part of a WPRTF to overwrite a complete other RTF, but now I need to take a number of WPRTF's and push them back into one WPRTF. I am sure I am doing something stupid, but I just can't work out how to in one line of code.

    I have tried lots of things and the most successful was appendSection, but this did as it said and created a new page / section. I just want somethign like:

    WPRTF.APPEND(wprtfSource.AsAnsiStr()) or whatever.

    Cheers

  • Zitat


    WPRichText1.CPPosition := MaxInt;
    WPRichText1.SelectionAsString := WPRichText2.AsString


    That actually worked really well, which I was surprised about as I was expecting WPRichText2.AsString to return an unformatted string.

    But, my next problem is that some of the formatting is changing. I am sorry that this may seem confusing, but here goes.
    My original letter generation:
    - opened a rtf
    - searched for <*> and "asked" my system to provide data
    - It then replaced the selection with whatever the system returned or "unknown" if it couldn't find anything.
    -Repeat until no fields left.

    So I have enhanced this as I think I have described elsewhere to:
    - load RTF
    - Search for <selection>, if found, split the rtf into other rtf's to show the user and the user selects which bits they want included. The formatting looks fine in these.
    - Once the user it happy, they say OK and the RTF's are sucked back into the original RTF (the original RTF is cleared first).
    - Now back to the routine above to replace the fields.

    But when the rtf is finally saved, any field that started at the beginning of a line has had its format removed and changes back to Times New Roman. I tried adding a couple of chars at the start of the line and it is fine, only for fields at the start of the line. These fields are in the middle of the blocks that have been sliced and re-put back together.

    Does this make sense?


    I saved the RTF right before I start swapping field values and it still looks fine, it is just when the fields are substituted.

    Sorry for the long post

    JAC.

    • Offizieller Beitrag

    Hi,

    Zitat

    That actually worked really well, which I was surprised about as I was expecting WPRichText2.AsString to return an unformatted string.


    It works like to TField.AsString, simply read contents as single byte string. Also see function AsANSIString - there you can provide a format string to select the format you want to read. Property "Text" will read unformattted text, or, better, AsANSIString('ANSI')

    Typical mailmerge - check out the method ReplaceTokens.

    Zitat

    But when the rtf is finally saved, any field that started at the beginning of a line has had its format removed and changes back to Times New Roman. I tried adding a couple of chars at the start of the line and it is fine, only for fields at the start of the line. These fields are in the middle of the blocks that have been sliced and re-put back together.

    Does this make sense?

    This depends on how you insert the text. If you use mail merge the format should not change. The inserted text should have the format of the text representing the "insert point"

    Julian

  • Again, this must be getting tedious for you, but it seems impossible for me to inject formatted text from on WPRTF into another - I am sure I am missing something.

    Zitat


    This depends on how you insert the text. If you use mail merge the format should not change. The inserted text should have the format of the text representing the "insert point"


    I specifically want the inserted text to have the format it had in the source WPRTF. I am substituting a token or in this case a block of text with paragrahs, tables, bold, etc. etc. So I want to basically take WPRTFSource and inject its contents into WPRTFDest at the "insert point" retaining formatting.


    The first bit does seem like typical mailmerge and you have suggested using replaceTokens, which I fully intend researching and am looking at the help for now. Unfortunately my original mailmerge code is written and working and deals with nRows e.g. main letter has customer with a table on it with one row per order. I have not seen how to do that without the reporting tool.

    I am not sure how I would deal with:


    Using mailmerge.

    Thank you for your time. My support will quieten down soon I am sure.

    • Offizieller Beitrag

    Hi,

    Zitat

    I specifically want the inserted text to have the format it had in the source WPRTF. I am substituting a token or in this case a block of text with paragrahs, tables, bold, etc. etc. So I want to basically take WPRTFSource and inject its contents into WPRTFDest at the "insert point" retaining formatting.

    first you need to select the text you want to replace (SetSelPosLen),

    then you set the cursor position to the start of it,

    delete the text,

    and assign to property SelectionAsString the text you want to have there. You need of course assign RTF code, not just plain text!

    The mailmerge works just alike, internally also the field is selected while OnMailMergeGetText is executed.

    Julian

  • Just e-mailed a response with full details etc., you are trying to help, but I don't think I am explaining myself well enough. Could you take a look at the e-mail and let me know as soon as possible.

    I need to get this rolled out this weekend.
    Cheers,
    JAC

    • Offizieller Beitrag

    Hi,

    thanks for your mail.

    I now posted a demo
    https://www.wpcubed.com/ftp/ex/replacetokens.zip

    which compares mail merge and traditional search&replace.
    With mail merge you have much more possibilities and usually
    the process does not destroy the document.

    To save the document without fields use
    SaveToFile(..., 'AUTO-nomergefields');

    I hope this helps.

    Julian

    PS.: The problem you see appears to be introduced by the default document font. The inserted text uses the default fonts, in this case Times New Roman, 11 pt instead of the Arial 12. Since the field is at the start of the text and you do not use the mailmerge feature
    (it would handles this case - see my code) the paragraph format from the
    loaded text overrides the destination. This is what is
    usually expected when you do copy&paste into an empty line.
    In the demo I dont see this problem though. Please do a WP.DefaultAttr.Clear to make sure you do NOT use a default
    font.

  • Julian,
    Thanks for taking the time to look at my stuff and post a memo. The demo demonstrates the subtle difference really well. I still have a lot to learn with the components.

    I am now using the replace tokens and mailmerge code. So far so good and it satisfies on of my main design goals, i.e. not to put proprietary stuff in the RTF's so they can edit them in whatever they want.

    So to see if you can see an elegant solution to the following. If I want a 1-> n relationship to be displayed in a document (e.g. one client with a list of their investments), I insert a table into the document and then have something like the following.

    Code
    <repeatrow><inv>	<inv><inv>


    My code used to be:

    Code
    if StrStartsWithLeftNoCase(cRepeatRowPrefix,theToken) then      begin        delete(theToken,1,length(cRepeatRowPrefix));        WPRichMain.seltext:='';        RepeatRow(theToken);      end

    Which basically deletes the token, works out which table is to be used and repeat the row for each record, then you have n rows.

    I haven't looked at this code for ages, but if it gets called in MergeGetText() it doesn't work, you only get the one row. Looking at the code, it would be nice if it didn't use the clipboard either. I guess I could use the AsString of the selection for this.

    So is it a no-no to mess with cursors and content in the MergeGetText() and if so, do you propose I pre-parse the text before replaceTokens it, or replaceTokens it and then search for fields starting with <repeatrow*?

    Thanks again, you have really speeded up the trouble shooting on this.
    [/quote]

  • So I have templates with:
    <repeatrow><inv> <inv><inv>

    I used to delete the repeatrow token and then execute:

    vNoRows:=FCallBackTableRows(vTableName);
    WPRichMain.SelectThisRow;
    WPRichMain.selstart:=WPRichMain.selstart+Offset;
    vrowData:=WPRichMain.SelText;
    WPRichMain.CopyToClipboard;
    dec(vNoRows);
    while vNoRows >= 1 do
    begin
    WPRichMain.InsertRow;
    WPRichMain.SelLength:=0;
    WPRichMain.TableRowNumber:=WPRichMain.TableRowNumber+1;
    WPRichMain.TableColNumber:=0;
    WPRichMain.ShowCursor;
    WPRichMain.PasteFromClipboard;
    WPRichMain.ShowCursor;
    WPRichMain.CPPosition:=WPRichMain.CPPosition-1;
    dec(vNoRows);
    end;
    WPRichMain.SelLength:=0;

    which resulted in:
    <inv> <inv><inv>
    <inv> <inv><inv>
    <inv> <inv><inv>
    etc, which worked fine.

    Now, I end up with:
    repeatrow:invinv.longdescription inv.amountinv.next
    repeatrow:invinv.longdescription inv.amountinv.next
    repeatrow:invinv.longdescription inv.amountinv.next

    where each is infact a field, but none of them get replaced, so I have 2 problems:
    * WPRichMain.selstart:=WPRichMain.selstart+Offset;
    where offset is the token, still has the whole row selected, so I get n repeatRows, which will lead to an infinite loop if the fields got evaluated
    * The fields in the table don't get evaluated.

    So should I:
    1) Pre-parse the document looking for repeatrows and explode these before swapping for tokens (could see a problem with nesting, possibly)
    2) Am I doing the n-row duplication in a really stupid way?

    Cheers Julian - and don't lose faith, I will get there and start to help others!
    JAC.

  • Julian,
    So I have it working now. I search for special tokens before issuing
    WPRichMain.ReplaceTokens('<','>');
    i.e.
    found:=WPRichMain.Finder.Next('<'+cRepeatRowPrefix+'*>');
    & cCommandPrefix. Not as elegant as your options.

    I still feel a little in the dark about a lot of the tool, but thanks to you, this is up and running. I hope I have time to really learn the componentset.

    Thanks again.
    JAC.

    • Offizieller Beitrag

    Hi,

    now I am a bit lost :-) - if you use < > signs in this NG please select the check box "Disable HTML" to avoid truncation.

    Zitat

    So is it a no-no to mess with cursors and content in the MergeGetText() and if so, do you propose I pre-parse the text before replaceTokens it, or replaceTokens it and then search for fields starting with <repeatrow*?

    Yes, this was a 100% no-no with WPTools 4, with WPTools 5 ist is a bit better. Since inside the OnMailMergGettext event the cursor is inside the field and the field is selected you actually CAN use InputString and also LoadFromFile. But you may not use methods which change the paragraph tree, such as InsertRow.

    If you need to insert a dynamically created table you have 2 options:
    a) create the table in a different TWPRichText and insert the result "AsString"
    b) insert a marker, such as @@@1@@@, and, in a second path, locate this marker and insert the table using TableAdd.

    The use of "WPRichMain.CopyToClipboard" is rather ugly, you can also use s := SelectionAsString and load this text using the same property when required.