Hi,
I am using the mail merge feature. In this process in the
word document I am using around 20 tables with mail merge they will be shown if
the data is present otherwise they won’t be shown.
I will show you one of my table structures
«TableStart:xyz»
Some header here
Column1 |
Column2 |
«TableStart:abc»«Column1» |
«Column2»«TableEnd:abc» |
«TableEnd:xyz»
In the above I am using two tables
reason is I do not want show the above table if the data is not present hence I
had added the outer table. Like the above I am having 20 tables in my word
document.
And I want to give the one single
line space between the tables
Up to this everything works fine
with the data.
Problems are as follows
- In the above I had used the outer most TableStart and TableEnd so they are also occupying the one line space after data is populated but I do not want that how to remove it?
- I already mentioned that I am having around 20 tables if some of the tables do not contain any data during that situation I am getting blank pages in the middle of the document how to remove them? In order to fix the above I tried the below things
CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions;
CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;
With the above I am able to do that, but the above two statements are causing the below problems they are as follows
- They are removing my single line space between
the tables also. And spaces which I gave intentionally in my document they are
also getting removed. (My problem is very simple I want remove the outer most tablestart
and tableend line spaces that’s it I do not remove any line space in the whole
document which I gave intentionally)
- They are removing the page breaks which I gave intentionally they should not be removed. Please let me know how handle the above two.
Thanks,
Burepalli V S Rao.
Hi Burepalli,
Thanks for your inquiry. Please read following documention links for your kind refernece. Hope this helps you.
https://docs.aspose.com/words/net/mail-merge-and-reporting/
https://docs.aspose.com/words/net/clean-up-before-or-during-mail-merge/
https://docs.aspose.com/words/net/mail-merge-and-reporting/
If you still face problem, please share following detail for testing. I will investigate the issue on my side and provide you more information.
- Please supply us with the input document
- Please supply us with the datasource in the form of XML
- Please supply us with the output document showing the undesired behavior
- Please supply us with the expected document showing the desired behavior (You can create this document using Microsoft Word).
Hi tahir.manzoorv,
thanks for your reply. what ever you mentioned i tried all the things. but still i am experiencing the same issue.
please answer this question rest i will let you know
Column1 |
Column2 |
«TableStart:abc»«Column1» |
«Column2»«TableEnd:abc» |
Column1 |
Column2 |
«TableStart:abc»«Column1» |
«Column2»«TableEnd:abc» |
the bove is my table, my objective is using merge field concept in the word document i need to populate the above.
if there is no data then i do not want see the above table at all in my word document(i do not want column headers also) i do not know how do that. if there is a data i need to see the table with data(this i know)
can you please let me know.
Thanks,
Burepalli V S Rao.
Hi Burepalli,
Thanks for your inquiry. Please use the following code snippe to remove the tables which have no data after mail merge. Hope this helps you. Please let us know if you have any more queries.
Document doc = new Document(MyDir + "Template.docx");
DataTable dt = new DataTable("test");
dt.Columns.Add("test", typeof(string));
dt.Rows.Add("");
doc.MailMerge.ExecuteWithRegions(dt);
foreach (Table table in doc.GetChildNodes(NodeType.Table, true).ToArray())
{
// Remove empty rows
Node[] rows = table.Rows.ToArray();
foreach (Row row in rows)
{
bool removeRow = true;
foreach (Cell cell
in row.Cells)
{
if (cell.FirstParagraph != null)
{
removeRow = !cell.FirstParagraph.HasChildNodes;
if (removeRow == false)
break;
}
}
if (removeRow)
row.Remove();
}
// Remove the header of table by reomving the table node
// as this is the one row in the table
// You can change following lines of code according to your requirements
if (table.FirstRow == table.LastRow)
table.Remove();
}
doc.Save(MyDir + "Out.docx");
Hi Tahir Manzoor
thanks for your reply. but the above code is not working. i need the working code.
Thanks,
Burepalli V S Rao.
Hi Burepalli,
Thanks for your inquiry. Could you please share following details for testing? I will investigate the issue on my side and provide you more information.
- Please supply us with the input document
- Please supply us with the datasource in the form of XML
- Please supply us with the output document showing the undesired behavior
- Please supply us with the expected document showing the desired behavior (You can create this document using Microsoft Word).
Hi
tahir.manzoorv,
Thanks for
your reply. As per the previous mail i am sending the
My word
document without replacing the data and with replacing the data along with that
i am sending my data in another word document.
Please check
those things and please let me know what i am missing.
Thanks,
Burepalli V
S Rao.
Hi Hi tahir.manzoorv,
did you checked the above?
Thanks,
Burepalli V S Rao.
Hi tahir.manzoorvv,
any bug in aspose word related to the above?
Thanks,
Burepalli V S Rao.
Hi Burepalli,
Please accept my apologies for late response.
Thanks for sharing the document. The template document (Before Replacing the data.doc) does not contain the MS Word table as shown in your following post (see the attached image):
https://forum.aspose.com/t/52268
Unfortunately, it is difficult to say what the problem is without the documents. Please create a simple application (for example a Console Application Project) that helps us reproduce the same problem on our end and attach it here for testing. Unfortunately, it is difficult to say what the problem is without the Document(s) and simplified application. We need your Document(s) and simple project to reproduce the problem. As soon as you get these pieces of information to us we’ll start our investigation into your issue.
It would be great if you please share your final output Doc/Docx which shows the data inside MS Word table.I will then provide you more information on this along with code.
Hi tahir.manzoorv,
reason for sending the new document with different format is initially my client asked me the data in the table format hence previously i sent that but now he is asking me in different format hence now i changed the format. but here the thing is what ever might be the format still the issue is present in the both the formats.
so please ignore my previous documents, please look at the new documents which i am attaching along with that i am attching one image with the screen shot which shows the issue i am talking about.
and i am sending the method which does the word processing also that is as follows
the below is the simple method i am using for doing the mail merge
public
void MailMerge(int folderId)
{
// Load the template document.
var doc = new Aspose.Words.Document(@"C:\123\Before replacing the data.doc");
var resultantDataSet = new DataSet();
// Currently i am hard coding my data, sometimes this data might be there or not
var table = new DataTable { TableName = "2" };
table.Columns.Add(
"3", typeof(string));
table.Columns.Add(
"4", typeof(DateTime));
table.Columns.Add(
"5", typeof(int));
table.Rows.Add(
"wwwwwwwwwwwww", DateTime.Now, 100);
table.Rows.Add(
"eeeeeeeeeeeeee", DateTime.Now, 100);
if (table.Rows.Count > 0)
{
resultantDataSet.Tables.Add(table);
var dt = new DataTable("1");
var dr1 = dt.NewRow();
dt.Rows.Add(dr1);
resultantDataSet.Tables.Add(dt);
}
var table1 = new DataTable { TableName = "7" };
table1.Columns.Add(
"abc", typeof(string));
table1.Columns.Add(
"def", typeof(DateTime));
table1.Columns.Add(
"ghi", typeof(int));
table1.Rows.Add(
"wwwwwwwwwwwww", DateTime.Now, 100);
table1.Rows.Add(
"eeeeeeeeeeeeee", DateTime.Now, 100);
if (table1.Rows.Count > 0)
{
resultantDataSet.Tables.Add(table1);
var dt = new DataTable("6");
var dr1 = dt.NewRow();
dt.Rows.Add(dr1);
resultantDataSet.Tables.Add(dt);
}
doc.MailMerge.CleanupOptions =
MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.ExecuteWithRegions(resultantDataSet);
doc.Save(
@"C:\123\After replacing the data.doc");
}
the above method is very straight forward and issue is also very straight forward. please let me know how to fix the issue.
Thanks,
Burepalli V S Rao.
Hi Burepalli,
Thanks for sharing the detail.
In your case, I suggest you please use MailMerge.CleanupOptions with MailMergeCleanupOptions.RemoveEmptyParagraphs flag to remove empty Paragraphs. Following code snippet shows how to instruct the mail merge engine to remove any empty paragraphs. I have attached the output Doc file with this post for your kind reference.
doc.MailMerge.CleanupOptions
|= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedRegions;
// doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedFields;
// doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.ExecuteWithRegions(resultantDataSet);
doc.Save(MyDir + @"Out.doc");
I suggest you please read following documentatoin links for your kind refernece.
https://docs.aspose.com/words/net/clean-up-before-or-during-mail-merge/
https://reference.aspose.com/words/net/aspose.words.mailmerging/mailmergecleanupoptions/
Hi tahir.manzoor,
Thanks for your immediate response. reason for not using the below statement is
doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveEmptyParagraphs;
it worked with one problem, the problem is it is removing the page breaks which are present in my word document which i gave intentionally.i do not want to remove the page breaks which i gave intentionally but it was removing how to avoid that.
one more problem in my word document i am using
doc.Range.Replace
with the above i am getting the below issue
The replace string cannot contain special or break charactersThe replace string cannot contain special or break characters.
i am attaching my word document with page break in it and after replacing the data the page break is getting removed with the above same code and after adding your two statements to it.
please let me know how to fix the above two problems.
Thanks,
Burepalli V S Rao.
Hi tahir.manzoor,
any update on the above?
Thanks,
Burepalli V S Rao.
Hi Burepalli,
Thanks for your inquiry.
When the MailMerge.RemoveEmptyParagraphs property is set to true any paragraphs which are empty or only contain TableStart or TableEnd merge fields are removed automatically during mail merge. In this situation if you are applying the techniques to such a paragraph then this will cause incorrect behavior as the paragraphs containing page breaks will be removed during mail merge.
Please use the following code snippet to remove empty paragraphs after mail merge. Hope this helps you.
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.ExecuteWithRegions(resultantDataSet);
// Remove the paragraphs from the document if they are empty after mail merge.
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in paragraphs)
{
if (para.GetText().Contains(ControlChar.PageBreak))
continue;
else if (para.GetText().Trim() == "")
para.Remove();
}
doc.Save(MyDir + "out.doc");
Hi tahir.manzoor,
Regarding the page break:
thanks you so much for your immediate response. it fixed the page break issue but still i will check different possible scenarios i will let you know still if see any problem.
Regarding the Replace method:
but what about my second problem.
i.e., in my word document i am using
doc.Range.Replace
with the above i am getting the below issue
The replace string cannot contain special or break charactersThe replace string cannot contain special or break characters.
Thanks,
Burepalli V S Rao.
Hi tahir.manzoor
Regarding
the page break issue:
Thank
you so much for your immediate response. It fixed the page break issue but
still i will check different possible scenarios and i will let you know still
if i see any issue.
Regarding
the Replace method:
i.e., in my word document i am using
doc.Range.Replace
With the above i am getting the
below issue
The replace string
cannot contain special or break characters.
What i have to do
in order to come out of the above issue.
Thanks,
Burepalli V S Rao.
Hi Burepalli,
Please accept my apologies for late response.
Unfortunately, you cannot use special character in replacement and captured strings. “An exception is thrown if a captured or replacement string contain one or more special characters: paragraph break, cell break, section break, field start, field separator, field end, inline picture, drawing object, footnote.”
In future we will consider improving Replace method in order to support special characters. Your request has been linked to the appropriate issue (i.e. WORDSNET-1252) in our bug tracking system and you will be notified as soon as it is resolved. We apologize for your inconvenience.
Could you please share your code along with document for which you are getting this error? I will investigate the issue on my side and try to provide you a workaround.
Hi ,
Thanks for your reply.
coming to the first issue only for that this is the code you have given
// Remove the paragraphs from the document if they are empty after mail merge.
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in paragraphs)
{
if (para.GetText().Contains(ControlChar.PageBreak))
continue;
else if (para.GetText().Trim() == "")
para.Remove();
}
with the above there is a problem, the problem is as follow for the above same document if i add the one empty line space between the Test1 and Test 2 they are also getting removed or the message which i entered contains the empty line space then that also getting removed but which is not correct.
you should remove only the empty spaces which you have added.
how to avoid the above please let me know. the same you can verify it in the above documents which i gave.
Thansk,
Burepalli V S Rao.
Hi tahir.manzoorv,
any update on the above?
Thanks,
Burepalli V S Rao.