MailMerge - is it possible to repeat data inside one paragraph?

Hi!
let’s say I have a table with two columns Col1 and Col2 and 3 rows of data
Currently if I have:
-
Each table row will create new paragraph or if merge fields are inside table row I will get word table with 3 rows.
What I am looking to do is generate a word document that would have one paragraph in the format Col1 - Col2 / Col1 - Col2
so if my table has:
C1 C2
--------
A B
C D
E F
I would get one paragraph: A-B C-D E-F
Joining data in the database is not an option since merge fields have to be formatted differentsly (different font type/font color)
Thanks,
Igor

Hi,
Due to the strucure of a word document ,all fields needs to be included inside a paragraph. This means when the regions are populated it adds them as separate paragraphs. The only way around this that I know of, would be to combine each of the generated mail merge paragraphs together.
The code below does this, although you will have to edit it a little so only the paragraphs you want (from the mail merge) are combined togehter (or else all paragraphs will be combined).

Document doc = new Document(dataDir + "Test Merge.doc");
doc.MailMerge.ExecuteWithRegions(Data);
// Gather all paragraphs , you can change doc to a different node to only combine a certain sections paragraphs etc
NodeList paras = doc.SelectNodes("//Paragraph");
foreach(Paragraph para in paras)
{
    // First paragraph will have the others joined to it
    Paragraph basePara = (Paragraph) paras[0];
    if (para != basePara)
    {
        // Add a space inbetween joined paragraphs
        if (basePara.LastChild.NodeType == NodeType.Run)
        {
            Run run = (Run) basePara.LastChild;
            run.Text += " ";
        }
        // Join all nodes from that paragraph to the base
        while (para.ChildNodes.Count != 0)
        {
            basePara.AppendChild(para.ChildNodes[0]);
        }
        // Remove the empty paragraph
        para.Remove();
    }
}

If you need any further assitance, please don’ t hesitate to ask

Thanks,

Hi Igor,

Thank you for your interest in Aspose.Words. I think, in your case you can try using MergeField event handler to achieve this. Please see the following forum thread for more information:
https://forum.aspose.com/t/72092
Also, in one of future versions of Aspose.Words we will add more Mail Merge syntax and you will be able to repeat only part of a paragraph during mail merge. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.
Best regards.

aske012:
Hi,
Due to the strucure of a word document ,all fields needs to be included inside a paragraph. This means when the regions are populated it adds them as separate paragraphs. The only way around this that I know of, would be to combine each of the generated mail merge paragraphs together.
The code below does this, although you will have to edit it a little so only the paragraphs you want (from the mail merge) are combined togehter (or else all paragraphs will be combined).
Document doc = new Document(dataDir + “Test Merge.doc”);
doc.MailMerge.ExecuteWithRegions(Data);
// Gather all paragraphs , you can change doc to a different node to only combine a certain sections paragraphs etc
NodeList paras = doc.SelectNodes(“//Paragraph”);
foreach (Paragraph para in paras)
{
// First paragraph will have the others joined to it
Paragraph basePara = (Paragraph)paras[0];
if(para != basePara){
// Add a space inbetween joined paragraphs
if (basePara.LastChild.NodeType == NodeType.Run)
{
Run run = (Run)basePara.LastChild;
run.Text += " ";
}
// Join all nodes from that paragraph to the base
while(para.ChildNodes.Count != 0)
{
basePara.AppendChild(para.ChildNodes[0]);
}
//Remove the empty paragraph
para.Remove();
}
}
If you need any further assitance, please don’t hesitate to ask
Thanks,

Thanks! It looks like it might work. Could you please suggest a way for me to mark the paragraphs so that I can find and merge only the paragraphs that I need. Also in my case the paragraphs are located as follows:
- some delimiter
so there would be many occurences of groups of paragraphs that need to be merged together.

*alexey.noskov:
Hi Igor,
Thank you for your interest in Aspose.Words. I think, in your case you can try using MergeField event handler to achieve this. Please see the following forum thread for more information:
https://forum.aspose.com/t/72092
Also, in one of future versions of Aspose.Words we will add more Mail Merge syntax and you will be able to repeat only part of a paragraph during mail merge. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.
Best regards.

Thanks! I’ve replied in the thread you’ve mentioned.

Hi Igor,
As Alexey said, we are working to include merging while only repeating part of a paragraph. Right now though there is no way to achieve that with regions. You can try to implement Alexey’s suggestion, but it does require the data to be merged through simple mail merge, not using regions. This means you would have to restructure your regions into simple mail merge but since you don’t want to edit the data structure itself this may not be possible.
For these reasons you might want to try my method that I suggested above, it may save some time. To mark only certain parts of text you may want to use bookmarks. Be aware, when you bookmark a selection of merge fields, when the data is merged the book mark will only be selected over the first merged entry of data. So to get around this you should use something like this:
Bookmark1
Bookmark2
You can then select all of the content between Bookmark1 and Bookmark2 and run the code on it.
For information about working with bookmarks, see the page Working with Bookmarks here
An example that Alexey wrote of how to extract the content between two bookmarks which should be helpful can be found here (it is written in VB though).
Thanks,

Hi Igor,

Thanks for being patient.

Regarding WORDSNET-3197, it is to update you that we have closed this issue. We have introduced a new LINQ Reporting Engine which is a part of the Aspose.Words API that enables you to build reports using an extended set of reporting features. The engine enables you to reference business objects of your application in report templates directly, which agrees well with Domain-Driven Design widely used in modern software development. Moreover, the engine uses a subset of C# language in its template syntax. These features enable you as a developer to compose report templates in a familiar and intuitive way.

For example, using this new reporting engine you can repeat part of a Paragraph as follows:

DocumentBuilder builder = new DocumentBuilder();
builder.Write("The items are: <><>, <>and others.");
Document doc = builder.Document;
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, new string[]
{
    "Item1",
    "Item2",
    "Item3"
}, "items");
doc.Save("out.docx");

For more details, please visit this article:
https://docs.aspose.com/words/net/linq-reporting-engine-api/

I hope, this helps.

Best regards,

The issues you have found earlier (filed as WORDSNET-3197) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(3)