How to Replace Cr as CrLf

Hi, To all,

I have a question how to Replace Cr as CrLf.
There are many Word documents, and each doc has many special chars such as soft return, i.e., NewLine char. I use the below codes to replace all the Cr as CrLf,but it doesn’t work.

Dim Doc As Aspose.Words.Document = New Aspose.Words.Document(sFile)
Doc.Document.Range.Replace(Aspose.Words.ControlChar.Cr, Aspose.Words.ControlChar.CrLf, False, False)
Doc.Document.Range.Replace(vbNewLine, vbCrLf, False, False)
Doc.Document.Range.Text.Replace(Aspose.Words.ControlChar.Cr, Aspose.Words.ControlChar.CrLf)
Doc.Document.Range.Text.Replace(vbNewLine, vbCrLf)
Dim builder As New Aspose.Words.DocumentBuilder(Doc)
On Error Resume Next
builder.Document.Save(OutFile, Aspose.Words.SaveFormat.Doc)

However,there is still too many vbNewLine chars in the main document, that is to say, the replacement operation doesn’t work.
How to implement this replacement operation so that all the Cr chars in the main body document can be replaced with CrLf?

Thanks in advance!

Ducaisoft

@ducaisoft

Thanks for your inquiry. Please upgrade to the latest version of Aspose.Words for .NET 18.11 to get the desired output. We suggest you please use the new APIs of find and replace feature. We suggest you please read the following article.
Find and Replace

I have upgraded the dll to the latest one version 18.11, but it still failed! the replacement operation doesn’t work. The code is as follow:

    Dim Doc As Aspose.Words.Document = New Aspose.Words.Document(InputDocFile)
    Dim Builder As Aspose.Words.DocumentBuilder = New Aspose.Words.DocumentBuilder(Doc)
    Dim FindOptions As New Aspose.Words.Replacing.FindReplaceOptions
    FindOptions.Direction = Aspose.Words.FindReplaceDirection.Forward
    FindOptions.MatchCase = False
    Dim n As Long = Doc.Document.Range.Replace(New System.Text.RegularExpressions.Regex("[\n]"), Aspose.Words.ControlChar.ParagraphBreak, FindOptions)
    n = Doc.Document.Range.Replace(New System.Text.RegularExpressions.Regex("[\r]"), Aspose.Words.ControlChar.ParagraphBreak, FindOptions)
    n = Doc.Document.Range.Replace(New System.Text.RegularExpressions.Regex("[\r\n]"), Aspose.Words.ControlChar.ParagraphBreak, FindOptions)
    n = Doc.Document.Range.Replace(New System.Text.RegularExpressions.Regex(vbNewLine), Aspose.Words.ControlChar.ParagraphBreak, FindOptions)
    n = Doc.Document.Range.Replace(New System.Text.RegularExpressions.Regex(vbCr), Aspose.Words.ControlChar.ParagraphBreak, FindOptions)
    n = Doc.Document.Range.Replace(New System.Text.RegularExpressions.Regex(Chr(10)), Aspose.Words.ControlChar.ParagraphBreak, FindOptions)
    Builder.Document.Save(OutPutFile,Aspose.Words.SaveFormat.Doc)

Could you please share a demo project based on VB.net 2008 and Aspose.Words.dll version 18.11 to implement the replacement of NewLine as CrLf?

@ducaisoft

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Please refer the demo files for investigating into the issue!

Thanks!
DCDocsReplacer.zip (4.0 MB)

@ducaisoft

Thanks for sharing the detail. You should use special meta-characters if you need to work with breaks:

  • &p - paragraph break
  • &b - section break
  • &m - page break
  • &l - manual line break

We suggest you please read the following article.
Replace Text using Meta-Characters

Please use the following code snippet to get the desired output.

sFile = Path & "Input File for Testing Replacements.docx"
OutFile = Path & "18.11.docx"
Dim Doc As New Aspose.Words.Document(sFile)
Dim Builder As New Aspose.Words.DocumentBuilder(Doc)
Dim ReplaceN1 As Long = Doc.Range.Replace(New System.Text.RegularExpressions.Regex("Lover"), "Love", FindOptions) 'This replacement will work.
Dim ReplaceN2 As Long = Doc.Range.Replace("&l", Aspose.Words.ControlChar.ParagraphBreak, FindOptions) 'This replacement will work.

On Error Resume Next
Builder.Document.Save(OutFile, Aspose.Words.SaveFormat.Docx)

Ok!
Thank you very much!
That works! It’s done!
In addition, Does the Aspose.words.dll version 14.2 support this function? if so, how to implement the replacement?

@ducaisoft

Thanks for your inquiry. This feature is not available in old versions of Aspose.Words. Please use the latest version of Aspose.Words.