Paragraph break

I suspect there is a really simple answer to this but I cannot find it.

I created a word document which contains a piece of text that looks like this

some text here%par%more text here

What I would like to do is replace %par% with an actual paragraph break

with the path to the file in a variable sPath - VB.NET code - the code I wrote is only 3 lines long:

oDoc = New Aspose.Words.Document(fileName:=sFile)
oDoc.Range.Replace(pattern:="%par%", replacement:=Aspose.Words.ControlChar.ParagraphBreak)
oDoc.Save(fileName:=sFile .... etc

I tried with Compatibility options

oDoc.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2016)
Dim oSO As Aspose.Words.Saving.OoxmlSaveOptions = New Aspose.Words.Saving.OoxmlSaveOptions
oSO.Compliance = Aspose.Words.Saving.OoxmlCompliance.Iso29500_2008_Transitional
oDoc.Save(fileName:=sFile, saveOptions:=oSO)

But none of it seems to work. It does replace the text, but instead of a paragraph break the replaced text is just a “soft line break” rather than an actual “paragraph break”.

What am I doing wrong here?

@rozeboosje Yes, you are right, the answer is simple. You should use a special metacharacter &p as a replacement. See remarks section to Range.Replace method.

You ca use the following VB.NET code:

Dim oDoc As New Document("C:\Temp\in.docx")
oDoc.Range.Replace(pattern:="%par%", replacement:="&p")
oDoc.Save(fileName:="C:\Temp\out.docx")
1 Like

Okay this is really strange. I tried it with two different pieces of text.

The first piece, it worked perfectly. The second piece, it replaced %par% with a soft line feed.

Could I send you a sample document?

@rozeboosje Yes, please attach the sample document here. I will check it on my side and provide you with more information.

Please note that forum attachments are accessible only to you as the topic starter and to Aspose staff.

1 Like

Oops. No need. Your first suggestion worked perfectly. In the second example I thought I had inserted some text%par%more text but in actual fact I had inserted

some text
more text

And once I realised what I had done and I corrected it everything started working perfectly.

Thank you for your help!

@rozeboosje I’m glad to hear that the problem has been resolved. Please feel free to contact us if you encounter any further issues. We will be happy to help.

1 Like