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

Free Support Forum - aspose.com

Wrong result Replacing text with formatted HTML using Replace method


I’m using Aspose.Words v13.5.0.0

I’m trying to create some routines to dynamically generate some word documents from a document guide (created earlier)

I’m using some special tokens (@@TokenName) in the text to identify the sections that i want to replace, but I’m getting incorrect results in the final file

Some of the replaced text are OK, but the other ones are outside of their places

NOTE: I include for you a sample project to reproduce this error


Thanks for your inquiry. Firstly, please read following documentation link for your kind reference.

I have modified the code of IReplacingCallback_Replacing, please use the following code to achieve your requirement. Hope this helps you. Please let us know if you have any more queries.

Private Class ReplaceWithHtmlEvaluator

Implements IReplacingCallback

’ Your code

’ Your code

’ Your code

Private Function IReplacingCallback_Replacing(ByVal e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing

Dim builder As New DocumentBuilder(CType(e.MatchNode.Document, Document))



’ This is a Run node that contains either the beginning or the complete match.

Dim currentNode As Node = e.MatchNode

’ The first (and may be the only) run can contain text before the match,

’ in this case it is necessary to split the run.

If e.MatchOffset > 0 Then

currentNode = SplitRun(CType(currentNode, Run), e.MatchOffset)

End If

’ This array is used to store all nodes of the match for further highlighting.

Dim runs As New ArrayList()

’ Find all runs that contain parts of the match string.

Dim remainingLength As Integer = e.Match.Value.Length

Do While (remainingLength > 0) AndAlso (currentNode IsNot Nothing) AndAlso (currentNode.GetText().Length <= remainingLength)


remainingLength = remainingLength - currentNode.GetText().Length

’ Select the next Run node.

’ Have to loop because there could be other nodes such as BookmarkStart etc.


currentNode = currentNode.NextSibling

Loop While (currentNode IsNot Nothing) AndAlso (currentNode.NodeType <> NodeType.Run)


’ Split the last run that contains the match if there is any text left.

If (currentNode IsNot Nothing) AndAlso (remainingLength > 0) Then

SplitRun(CType(currentNode, Run), remainingLength)


End If



’ Remove run nodes

For Each run As Run In runs


Next run

’ Signal to the replace engine to do nothing because we have already done all what we wanted.

Return ReplaceAction.Skip

End Function

Private Shared Function SplitRun(ByVal run As Run, ByVal position As Integer) As Run

Dim afterRun As Run = CType(run.Clone(True), Run)

afterRun.Text = run.Text.Substring(position)

run.Text = run.Text.Substring(0, position)

run.ParentNode.InsertAfter(afterRun, run)

Return afterRun

End Function

End Class

This code works perfect

Thank you very much

Hi there,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.