We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Error in Word 2007

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:

  1. ArrayList propertyStarts = new ArrayList();
  2. NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
  3. foreach (FieldStart start in starts)
  4. {
  5. if (start.FieldType == FieldType.FieldDocProperty)
  6. {
  7. propertyStarts.Add(start);
  8. }
  9. }
  10. foreach (FieldStart start in propertyStarts)
  11. {
  12. Node currentNode = start;
  13. Node fieldSeparator = null;
  14. while (currentNode.NodeType != NodeType.FieldSeparator)
  15. {
  16. currentNode = currentNode.NextSibling;
  17. currentNode.PreviousSibling.Remove();
  18. }
  19. fieldSeparator = currentNode;
  20. while (currentNode.NodeType != NodeType.FieldEnd)
  21. {
  22. currentNode = currentNode.NextSibling;
  23. }
  24. fieldSeparator.Remove();
  25. currentNode.Remove();
  26. }

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

Thanks for your inquiry. I tested your code on my side and it seems that all works fine. There were no errors.

I used the latest version of Aspose.Words (v5.2.1) for testing.

Best regards.

Prior to unlinking the fields, I have the following 2 lines of code. This seems to me to be very similar to another error I've been having as per <A href="https://forum.aspose.com/t/109311</A>. Yet it is somewhat different.</P> <P>WorkDocClone.AcceptAllRevisions();</P> <P>WorkDocClone.TrackRevisions = </FONT><FONT color=#0000ff size=2>false</FONT><FONT size=2>;</P></FONT>

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

Thanks for additional information. I managed to reproduce this issue. Please try using the following code:

doc.AcceptAllRevisions();

doc.TrackRevisions = false;

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.NodeType != NodeType.FieldEnd)

{

currentNode = currentNode.NextSibling;

currentNode.PreviousSibling.Remove();

}

if (currentNode.NodeType == NodeType.FieldSeparator)

{

fieldSeparator = currentNode;

while (currentNode.NodeType != NodeType.FieldEnd)

{

currentNode = currentNode.NextSibling;

}

fieldSeparator.Remove();

}

currentNode.Remove();

}

Hope this helps.

Best regards.

Thank you.