My document contains multiple merge fields that reference Custom Document Properties in the document.
Using word automation one can call the function of “.Unlink();” which would take all the fields and replace them with the physical text of there result.
My merge fields look as follows { DOCPROPERTY SomeField \* MERGEFORMAT }
Example attached…
Aspose.Words can update the values fo DOCPROPERTY fields using Document.Range.UpdateFields method. Total removal of this field substituting it with field result is not directly supported. It can be done by document node tree manipulation however. If you need to do it for some reason please state this reason here and I will try to compose the code that will do this for you.
Best regards,
Hi
Thanks for your request. I think that the following code could be useful for you.
// Open document
Document doc = new Document(@"Test064\in.doc");
// Set doc property value
doc.CustomDocumentProperties["SomeField"].Value = "my value";
// update fields in the document (DOCUMENTPROPERTY and DOCUMENTVALUE field will be updated)
doc.Range.UpdateFields();
// Get collection of FiledStart nodes
ArrayList propertyStarts = new ArrayList();
NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
foreach (FieldStart start in starts)
{
if (start.FieldType == FieldType.FieldDocProperty)
{
propertyStarts.Add(start);
}
}
// For each DOCUMENTPROPERTY Field Start
foreach (FieldStart start in propertyStarts)
{
Node currentNode = start;
Node fieldSeparator = null;
// Remove field code
while (currentNode.NodeType != NodeType.FieldSeparator)
{
currentNode = currentNode.NextSibling;
currentNode.PreviousSibling.Remove();
}
fieldSeparator = currentNode;
// Move to Field End
while (currentNode.NodeType != NodeType.FieldEnd)
{
currentNode = currentNode.NextSibling;
}
// Remove field separator
fieldSeparator.Remove();
// Romove field end
currentNode.Remove();
}
// Save document
doc.Save(@"Test064\out.doc");