MailMerge: Replace mail merge field with table

  • I would like to replace a mail merge field with a table. I do it like that:

    In

    Code
    OnGetTextToInsert(Sender: TObject; const inspname: String; const Contents: TWPMMInsertTextContents; var Done: Boolean)


    I use Contents.MergePar.AppendNewTable() and so on.

    This basically inserts the table but above the table the name of the merge field is visible. It seems that it left the paragraph with the mail merge field untouched and instead it appended a new paragraph and added the table to that new paragraph.

    I would rather like the table inside the paragraph where the merge field resides. I couldn't find an "InsertNewTable" instead of an "AppendNewTable". But maybe my whole approach is wrong.

    Can you please explain to me how I can insert a table in a mail merge?

    Many thanks in advance,

    Dirk.

  • I tried that already but it doesn't work. Setting this property makes the red << >> markers disappear but not the field name.

    I also tried ShowMergeFieldNames:=false which sounded like the right parameter but the fields names are still shown.

    So, still the question is: how to make the field name disappear?

    • Offizieller Beitrag

    Hi,

    I read your original post incorrectly.

    You used "Contents.MergePar.AppendNewTable() " which is the incorrect way to add a table inside a field because it does not add it INSIDE the field but under the paragraph which contained the field.

    You need to call the TWPRichText directly.

    First ClearSelection and then

    function AddTable(colcount, lincount: Integer; Border: Boolean = TRUE)
    : TParagraph;

  • I am afraid I still don't get it. Hier is my code:


    This at least doesn't show the merge field name anymore (thanks to ClearSelection) but still the table is appended below the merge field and does not replace the field itself.

    I assume it's either because I call "AppendNewTable" or I use ActivePar. I don't want to use the AddTable method because I want to create the table row by row in a loop.

    I would really appreciate if you could give me a few lines of code that show how you would implement OnGetTextToInsert.

    Regards,
    Dirk.

    • Offizieller Beitrag

    As I wrote, You need to call the TWPRichText directly : WPRichText1.AddTable.

    Example:

    Code
    procedure TForm2.WPRichText1MailMergeGetText(Sender: TObject;
      const inspname: string; Contents: TWPMMInsertTextContents);
    begin
      if inspname='TEST' then
      begin
          WPRichText1.ClearSelection;
          WPRichText1.AddTable(3,3,true);
      end;
    end;