Hi,
C# is a foreign language to me :) , and so I have translated the code snippet to VB.net.
In case there are any other VB'er's who are put off by the curly bracket, I have included the code below (please bear in mind that although this code works for me, it may still contain bugs). It might be useful if there are any Aspose people who want to look over it, and confirm/reject it!
' Define the desired formatting here.
Private Sub ApplyFormatting(ByVal Run As Run)
Run.Font.Color = System.Drawing.Color.Aqua
Run.Font.Bold = True
End Sub
Private matchedRun As Run
Private Function SearchAndFormatEvaluator(ByVal sender As Object, ByVal e As ReplaceEvaluatorArgs) As ReplaceAction
Dim run As Run = e.MatchNode
Dim para As Paragraph = run.ParentParagraph
' We are using ASCII Character 168 (inverted question mark) symbol for temporary purposes.
' Any symbol will do that is not special and is guaranteed not to be present in the document.
matchedRun = run
para.Range.Replace(New Regex(e.Match.Value), New ReplaceEvaluator(AddressOf RunReplaceEvaluator), False)
Dim subrun As Run = run.Clone(False)
Dim pos As Integer = run.Text.IndexOf(Chr(168)) '("\xbf")
subrun.Text = subrun.Text.Substring(0, pos)
run.Text = run.Text.Substring(pos + 1, run.Text.Length - pos - 1)
para.ChildNodes.Insert(para.ChildNodes.IndexOf(run), subrun)
Dim selectionRun As New Run(run.Document, e.Match.Value)
para.ChildNodes.Insert(para.ChildNodes.IndexOf(run), selectionRun)
ApplyFormatting(selectionRun)
' Let's remove run if it is empty.
If run.Text = "" Then
run.Remove()
End If
' No replace action is necessary - we have already done what we intended to do.
Return ReplaceAction.Skip
End Function
Private Function RunReplaceEvaluator(ByVal sender As Object, ByVal e As ReplaceEvaluatorArgs) As ReplaceAction
If e.MatchNode Is matchedRun Then
e.Replacement = Chr(168) '"\xbf"
matchedRun = Nothing
Return ReplaceAction.Replace
Else
Return ReplaceAction.Skip
End If
End Function