Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have word document, and in that document i have a table and also some data.
My tasks are:
1) For that table i need to add some rows at the end of the table.
2) Replace some text in the word document(Not in the table), with some other text.

I have done both of them.
and here is my code.

For table:
-----------
DocumentFormat.OpenXml.Wordprocessing.TableRow theRow = tab.Elements<documentformat.openxml.wordprocessing.tablerow>().Last();
for (int i = 0; i < (rcount - 1); i++)
{
DocumentFormat.OpenXml.Wordprocessing.TableRow rowCopy = (DocumentFormat.OpenXml.Wordprocessing.TableRow)theRow.CloneNode(true);
for (int j = 0; j < Columncells.Count(); j++)
{
DocumentFormat.OpenXml.Wordprocessing.TableCell cell = rowCopy.Descendants<documentformat.openxml.wordprocessing.tablecell>().ElementAt(j);
DocumentFormat.OpenXml.Wordprocessing.Paragraph parg = cell.Elements<documentformat.openxml.wordprocessing.paragraph>().First();
DocumentFormat.OpenXml.Wordprocessing.Run r = parg.Elements<documentformat.openxml.wordprocessing.run>().First();
DocumentFormat.OpenXml.Wordprocessing.Text tex = r.Elements<documentformat.openxml.wordprocessing.text>().First();
tex.Text = tex.Text.Trim('1') + (i + 2);
if (j == 0 && i == 0)
{
rowCopy.Descendants<documentformat.openxml.wordprocessing.tablecell>().ElementAt(j).Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(run));
}
else
{
rowCopy.Descendants<documentformat.openxml.wordprocessing.tablecell>().ElementAt(j).Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
}
}
tab.InsertBefore<tablerow>(rowCopy, theRow);
}

For replace of text:
--------------------

Regex regexText = new Regex("Helo");
docText = regexText.Replace(docText, "Hello");

Actually my problem is

By using the code which i used for table, replacing of the text in word document is not working, When i remove that part of code than, replace functionality is working fine.

Can anybody help me, at which place of the code it is preventing replace functionality.

Thanks in advance.
Posted

1 solution

The docText may be invalid after you change the document by add rows?
Stey by step debug to see the details may help you。
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900