Dinner Invitation demo

Hi there,
I’m working on a task which is smiliar to Dinner Invitation demo, but I need to change the content of the invitation for each Invitee. Please send me some sample or suggestion.
Thanks,
Saravanan

Hi
Thanks for your request. There is used mail merge feature. So you can change the template to your needs. Please see the following link to learn how to prepare template.
https://docs.aspose.com/words/net/mail-merge-template/
Also see the following link to learn more about mail merge.
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
Best regards.

Thanks for the reply.
Look into the Dinner Invitation Demo code attached…

//Open the template document
Document doc = new Document(System.IO.Path.Combine(DocPath, "DinnerInvitationDemo.doc"));
//Example of a very simple mail merge to populate fields only once from an array of values.
doc.MailMerge.Execute(
new string[] { "MyName", "MyTitle", "MyAddress1", "MyAddress2" },
new object[] { "James Bond", "Secret Agent", "MI5 Headquarters", "Milbank, London" });
//Mail merge customer details from DataTable to the document.
//Whole document content will be repeated for each record.
DataTable customers = ExecuteDataTable("SELECT TOP 5 * FROM AsposeWordCustomers WHERE Country = 'USA'");
doc.MailMerge.Execute(customers);
return doc;

In the above code in first “doc.MailMerge.Execute” is fixed data, and the second one has different invitee’s address.
My need is for each Invitees I should change the data/content.
Please send me some sample/Demo code.
Thanks,
Saravanan

Hello Saravanan!
I added a new merge field to the document from our demo. Please see my attachment. Its name is MessageBody. Now you can add appropriate values to the data source and try mail merge on the modified document:

new string[] { "MyName", "MyTitle", "MyAddress1", "MyAddress2", "MessageBody" },
new object[] { "James Bond", "Secret Agent", "MI5 Headquarters", "Milbank, London", "Hey, let’s have a party on Friday!" });

Hope this helps.
Regards,

Thanks for the quick reply.
My need is I should be able to change the message for each person/Invitees.
Question 1.
For example, if I want to designate parking spot, how to do that?
Hey, let’s have a party on Friday.
Note: Please park your car at parking spot # <>.
Question 2.
This code is from the Demo…

DataTable customers = ExecuteDataTable("SELECT TOP 5 * FROM AsposeWordCustomers WHERE Country = 'USA'");
doc.MailMerge.Execute(customers);

Here, the mail merge fields are Simple MailMerge, how can I add MergeRegion? Also I would like to show some data in tabular formate (TableStart:tbName and TableEnd:tbName).
Please send me a sample code.
Thanks,
Saravanan

Hi
Thanks for your inquiry.

  1. Just insert merge field and add field with same name to data source (DataTable for example) that you use for merging.
  2. If you want to dynamically grow portions inside the document, then you should use mail merge with regions. Please see the following links for more information.
    https://docs.aspose.com/words/net/types-of-mail-merge-operations/

Best regards.

See my sample code in the next reply…

Hi there,
Here with attaching my code and template. You can find my templat at “C:\Correspondence\Bin\WordTemplates”. I’m using VisualStudio TemaEdition’s UnitTesting. Please open the unit test and run the Master2(). You should see 3 documents in single file. For each document I need to add some information in Table format.
I did not add data for the table, please update my code as you wish.
Thanks,
Saravanan

Hi
Thanks you for additional information. I created sample application for you. Please see the attachment. I hope this could help you.
Best regards.

Hi Alexey,
thanks for the reply.
I looked at your sample, it runs fine but it did not meet my requirement. It just repeats the same data/content on all 10 pages. My need is each page has diferent data in table format.
For example let us take a phone company, which has 10 customers. Month end it generates invoice for all 10 customers with their call information in a tabular format. It creates and saves in a single file, then prints and mails.
The invoice/bill contains the following…
Mailing address which is different for each customer and call details and the total bill amount including the tax (this data also different for each customer).
Please send me a sampel for this scenario.
Thanks,
Saravanan

Hi
Thanks for your request. I think you can try using MergeField event handler and two templates to achieve this. I created sample application for you. See attachment.
Best regards.

Thanks Alexey,
your code helped me a lot to proceed with my task. I have a question.
How to do page break, I have multi pages and all document always should start on next ew page.
Let us say , each page is 1 and 1/2 pages. So my second document should start on 3rd page.
Please send me an sample.
Thanks,
Saravanan

Hi Alexey,
My user prefers to have everything in one single template including the table. Can you please send a sample with single template.
Thanks,
Saravanan

Hi
Thanks for your request.

  1. Please see the following link to learn how to control new pages during mail merge.
    http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/how-to-control-new-pages-during-mail-merge.html
  2. I modified my example a little. Please change code of MergeField event handler
static void MailMerge_MergeFieldAddTable(object sender, Aspose.Words.Reporting.MergeFieldEventArgs e)
{
    if (e.FieldName == "TablePlaceHoleder")
    {
        //Get data
        DataTable tableData = GetEntityInfo(_currentID);
        _currentID++;
        //Execute mail merge with regions
        e.Document.MailMerge.ExecuteWithRegions(tableData);
        Paragraph parentParagraph = e.Field.Start.ParentParagraph;
        //Remoce parent paragraph;
        parentParagraph.Remove();
    }
}

And try using the attached template.
I hope this could help you.
Best regards.

Thanks Alexey,
your code works with “TablePlaceHoleder” merge field,

  1. Is there any way to make it work without this field?
  2. I prefer to make ReadOnly on some paragraphs and I know to do this using “ProtectedForForms” on sections. But my user wants to create BeginReadOnly and EndReadOnly merge fields between the readonly paragraphs. How can I do that?
  3. Also I have few lines, assuem that 10 bullte points on the templat and they want to hide some of them. Once it is hidden, the hidden lines should not have blank spaces. How to do that?

Please send me some sample code.
Thanks,
Saravanan

Hi
Thanks for your request.

  1. Just remove this additional field from document and modify the code.
static void MailMerge_MergeFieldAddTable(object sender, Aspose.Words.Reporting.MergeFieldEventArgs e)
{
    if (e.FieldName == "Option10")
    {
        //Get data
        DataTable tableData = GetEntityInfo(_currentID);
        _currentID++;
        //Execute mail merge with regions
        e.Document.MailMerge.ExecuteWithRegions(tableData);
    }
}
  1. Unfortunately Aspose.Words does not support editable regions. So the one way to achieve this is using ProtectedForForms as you mentioned.
  2. You can use hidden font to achieve this. For example see the following code.
Document doc = new Document(@"Test037\in.doc");
doc.FirstSection.Body.Paragraphs[1].ParagraphBreakFont.Hidden = true;
foreach (Run run in doc.FirstSection.Body.Paragraphs[1].Runs)
{
    run.Font.Hidden = true;
}
doc.Save(@"Test037\out.doc");

Also you can just remove paragraph from document.

Document doc = new Document(@"Test037\in.doc");
doc.FirstSection.Body.Paragraphs[1].Remove();

doc.Save(@“Test037\out.doc”);
Best regards.

Hi Alexey,
Please find attached sample project.
I have questions on the attached project.

  1. User wants have Read-Only paragraph by adding BeginReadOnly and EndReadOnly tags.
  2. This project supports two tables in single word document. When there is no data for table that table should be hidden or removed from the document. Right now it shows the header row.
  3. There are 5 Optional paragraphs, your might choose 1 to few. In that case rest of the paragraphs should be removed from the document and there should be any blanks. Also the line sequence number should work fine.

Please update this project and let me know.
Thanks,
Saravanan

Hi
Thanks for your request.

  1. There is no way to mark protected paragraph using BeginReadOnly and EndReadOnly tags.
  2. You can try using the following code to remove empty table.
void MailMerge_MergeFieldAddTable(object sender, Aspose.Words.Reporting.MergeFieldEventArgs e)
{
    if (e.FieldName == "Option10")
    {
        DocumentBuilder builder = new DocumentBuilder(e.Document);
        //Get data
        DataTable tableData = new DataTable();
        tableData = GetEntityInfo(_currentID);
        //Execute mail merge with regions
        if (tableData.Rows.Count > 0)
            e.Document.MailMerge.ExecuteWithRegions(tableData);
        else
        {
            builder.MoveToMergeField(tableData.TableName);
            Node parentTable = builder.CurrentParagraph.GetAncestor(typeof(Table));
            if (parentTable != null)
                parentTable.Remove();
        }
        _currentID++;
    }
}
  1. You can just remove Paragraph from list. You can use bookmarks to identify optional paragraphs and the use DocumentBuilder to move cursor to these paragraphs and remove.

Best regards.