Free Support Forum - aspose.com

URGENT! Unlinking fields with Aspose.Words

Hello

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…

How can I unlink the files with Aspose?

Thanks
Aron

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,

We are going to need to do this for our project. Would you be able to give me the code for that? Thank you.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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");

Best regards.