I am using the following code to unlink all the fields in a document. The attached document throws an error (System.NullReferenceException: Object reference not set to an instance of an object) on line 17 of the code below. This code has worked fine for many documents, but for documents which are done using Word 2007, we receive this error. Thank you for your help with this.
Code to unlink fields:
- ArrayList propertyStarts = new ArrayList();
- NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
- foreach (FieldStart start in starts)
- {
- if (start.FieldType == FieldType.FieldDocProperty)
- {
- propertyStarts.Add(start);
- }
- }
- foreach (FieldStart start in propertyStarts)
- {
- Node currentNode = start;
- Node fieldSeparator = null;
- while (currentNode.NodeType != NodeType.FieldSeparator)
- {
- currentNode = currentNode.NextSibling;
- currentNode.PreviousSibling.Remove();
- }
- fieldSeparator = currentNode;
- while (currentNode.NodeType != NodeType.FieldEnd)
- {
- currentNode = currentNode.NextSibling;
- }
- fieldSeparator.Remove();
- currentNode.Remove();
- }