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

Free Support Forum - aspose.com

Object reference not set to an instance of an object Error when Converting fields of a particular type to static text

Hi,


I’m using the technique explained in the http://www.aspose.com/docs/display/wordsnet/How+to++Replace+Fields+with+Static+Text url, to convert fields of a particular type to static text.

That technique produce an Object reference not set to an instance of an object Error, when the Field of a particular type has not a FieldSeparator.

The ReplaceFields.doc attached file was used for my test scenario. It contains 2 fields : one for FieldIf type and another one for FieldListNum type

Error only occured when converting field of FieldListNum Type because there is no FieldSeparator for field

vb.net code used

Dim doc As New Document(dataDir & “ReplaceFields.doc”)

FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldIf)

FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldListNum)

doc.Save(dataDir & “ReplaceFields.doc”)

the FieldsHelper Class can be found in the http://www.aspose.com/docs/display/wordsnet/How+to++Replace+Fields+with+Static+Text link

How can i resolve that issue?

Any idea will help

thanks in advance


Hi Stephane,


Thanks for your inquiry. We are working over your query and will get back to you soon.

Best regards,

Thanks, I am looking forward to your response

Hi Stephane,


Thanks for being patient. The problem occurs because such fields do not have field values, and Microsoft Word calculates and displays their values on the fly. So, the only way to replace such fields with actual values is looping through all fields, and calculate the values of the “ListNum” fields. For example see the following code:

Dim doc As New Document(MyDir
& “ReplaceFields.doc”)

FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldIf)

UnlinkFields(doc)

doc.Save(MyDir & “out.doc”)


Public Sub UnlinkFields(ByVal doc As Document)<o:p></o:p>

' Create DocumentBuilder, it will be needed to update autonum fields.

Dim builder As New DocumentBuilder(doc)

Dim autoNumber As Integer = 1

Dim listNumber As Integer = 1

'Get collection of FieldStart nodes

Dim fieldStarts As NodeCollection = doc.GetChildNodes(Aspose.Words.NodeType.FieldStart, True)

'Get collection of FieldSeparator nodes

Dim fieldSeparators As NodeCollection = doc.GetChildNodes(Aspose.Words.NodeType.FieldSeparator, True)

'And get collection of FieldEnd nodes

Dim fieldEnds As NodeCollection = doc.GetChildNodes(Aspose.Words.NodeType.FieldEnd, True)

'Loop through all FieldStart nodes

For Each start As FieldStart In fieldStarts

' Insert value of Autonum fields.

If start.FieldType = FieldType.FieldAutoNum OrElse start.FieldType = FieldType.FieldAutoNumLegal OrElse start.FieldType = FieldType.FieldAutoNumOutline OrElse start.FieldType = FieldType.FieldListNum Then

builder.MoveTo(start)

Dim val As String = If(start.FieldType = FieldType.FieldListNum, String.Format("{0})", System.Math.Max(System.Threading.Interlocked.Increment(listNumber), listNumber - 1)), String.Format("{0}.", System.Math.Max(System.Threading.Interlocked.Increment(autoNumber), autoNumber - 1)))

builder.Write(val)

End If

'Search for FieldSeparator node. it is needed to remove field code from the document

Dim curNode As Node = start

While curNode.NodeType <> Aspose.Words.NodeType.FieldSeparator AndAlso curNode.NodeType <> Aspose.Words.NodeType.FieldEnd

curNode = curNode.NextPreOrder(doc)

If curNode Is Nothing Then

Exit While

End If

End While

'Remove all nodes between Fieldstart and FieldSeparator (of FieldEnd, depending from field type)

If curNode IsNot Nothing Then

RemoveSequence(start, curNode)

End If

Next

'Now we can remove FieldStart, FieldSeparator and FieldEnd nodes

fieldStarts.Clear()

fieldSeparators.Clear()

fieldEnds.Clear()

End Sub

'/

'/ Remove all nodes between start and end nodes, except start and end nodes

'/

'/ The start node

'/ The end node

Public Sub RemoveSequence(ByVal start As Node, ByVal [end] As Node)

Dim curNode As Node = start.NextPreOrder(start.Document)

While curNode IsNot Nothing AndAlso Not curNode.Equals([end])

'Move to next node

Dim nextNode As Node = curNode.NextPreOrder(start.Document)

'Check whether current contains end node

If curNode.IsComposite Then

If Not TryCast(curNode, CompositeNode).GetChildNodes(Aspose.Words.NodeType.Any, True).Contains([end]) AndAlso Not TryCast(curNode, CompositeNode).GetChildNodes(Aspose.Words.NodeType.Any, True).Contains(start) Then

nextNode = curNode.NextSibling

curNode.Remove()

End If

Else

curNode.Remove()

End If

curNode = nextNode

End While

End Sub


I hope, this helps.

Best regards,