I am trying find one or more paragraphs in a word document between some known tags and paste a copy of this further up the document. I then want to find any fields within the copied section and replace these with some alternative text.
Hi Fiona,
- Your input Word document.
- Your target/expected document showing the desired final output (you can use Microsoft Word to create this document)
I have attached an Input and Output document.
Hi Fiona,
Document(@“C:\Temp\input.doc”);
Paragraph startPara = null;
Paragraph endPara = null;
Paragraph targetPara = null;
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph paragraph in paragraphs)
{
if (paragraph.ToString(SaveFormat.Text).Trim().Equals(""))
startPara = paragraph;
if (paragraph.ToString(SaveFormat.Text).Trim().Equals(""))
endPara = paragraph;
if (paragraph.ToString(SaveFormat.Text).Trim().Equals(""))
targetPara = paragraph;
}
ArrayList content = ExtractContent(startPara, endPara, false);
foreach (CompositeNode node in content)
{
targetPara.ParentNode.InsertBefore(node, targetPara);
NodeCollection runs = node.GetChildNodes(NodeType.Run, true);
foreach (Run run in runs)
run.Font.Hidden = false;
NodeCollection fields = node.GetChildNodes(NodeType.FieldStart, true);
foreach (FieldStart fieldStart in fields)
{
Paragraph parentPara = null;
if (fieldStart.FieldType == FieldType.FieldQuote)
{
parentPara = fieldStart.ParentParagraph;
Field quoteField = fieldStart.GetField();
quoteField.Remove();
parentPara.Runs.Add(new Run(doc, "Your customer text"));
}
}
}
doc.Save(@"C:\Temp\out.docx");
Thanks for the quick response. This code looks very short and neat. I will make use of it.