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

Free Support Forum - aspose.com

Repeatative text replacement does not work

Hi Aspose team,

in attached project you can find code that is opening OneNote document, making text replacement and overwriting file. If you run it 2x there is problem.

After first run:

06.12.2021 11:25:10
Starting file: OneNoteText\OneNoteText.one
Original Text: Roadshow PPT Folder
New Text: Oliver Test Change
Original Text: Montag, 6. Dezember 2021
Original Text: 11:11
Original Text:
Original Text: Roadshow PPT Folder
New Text: Oliver Test Change
Saved file: OneNoteText\OneNoteText.one

06.12.2021 11:25:11
Duration in [ms]:998

After second run:

06.12.2021 11:25:19
Starting file: OneNoteText\OneNoteText.one
Error Message: Index and length must refer to a location within the string.
Parameter name: length
Error Stack Trace: at System.String.Substring(Int32 startIndex, Int32 length)
at :clubs: .:heart:(RichText :slight_smile:)
at :clubs: .:spades: :slight_smile:()
at :clubs: .:spades: :slight_smile:()
at :heart: .:spades: :slight_smile:()
at :slight_smile: .:spades: :slight_smile:()
at :spades: .:spades: :slight_smile:()
at :slight_smile: .:spades: :slight_smile:()
at :slight_smile: .:slight_smile:()
at :slight_smile: .:spades: :slight_smile:()
at :heart: ?.:slight_smile:(Stream :slight_smile:, Document :heart:, LoadOptions :clubs:)
at Aspose.Note.Document…ctor(Stream inStream, LoadOptions loadOptions)
at Aspose.Note.Document…ctor(Stream inStream)
at AsposeOneNote.OneNote.UpdateText(String Filename) in C:_Projects\Bugs\Aspose\0\Aspose.Bugs\OneNote.cs:line 65
06.12.2021 11:25:19
Duration in [ms]:548

To repeat locally make copy of OneNoteText - Original.one and save it as a OneNoteText.one. Document OneNoteText - Damaged.one is file where one change is already done.

Thanks for help,

OneNoteTextReplacement.zip (116.9 KB)


Please notice, after an initial test, I am able to reproduce the issue as you mentioned by using your sample document and sample code. I found an exception “Index and length must refer to a location within the string” on second attempt. I have logged a ticket with an id “NOTENET-5529” for your issue. We need to investigate it thoroughly if your code needs tweak or it is an issue with the API. We will look into it soon.

Once we have an update on it, we will let you know.


We investigated the issue further.
The reason of your issue is that after replacement RichText.Styles list is not updated according to text’s replacement. Let’s take a look at the example: RichText with “SomeTextAnotherText” text and two styles in RichText.Styles(see attached notenet5529.png).
notenet5529.png (6.8 KB)

Initially, RichText.Styles[0] defines style for “SomeText” and RichText.Styles[1] is for “AnotherText”.
If we run the code below:

richText.Text = richText.Text.Replace("SomeText", "NewText");

our richText will have “NewTextAnotherText” value that is 1 letter shorter than the original one.
And now Style[1].RunningIndex = 19 is invalid: it is too big for new text.

There are a couple of options to fix this issue:

  1. Update manually elements of richText.Styles list to have correct values for RunIndex property.
  2. The first simplest solution if it is appropriate: make replaced and replacement string of the same size. By using whitespaces, for example. Your sample works well with "Oliver Test Change " and “Roadshow PPT Folder”.
  3. The second simplest solution if it is appropriate: drop the formatting.
                        if (richText.ParagraphStyle != null)
                            richText.ParagraphStyle = ParagraphStyle.Default;

Please note, we are working on refactoring of public API for RichText element that will fix RunIndex property automatically. We plan to release it not later than in Aspose.Note for .NET 22.5 release.

Thanks for quick reply.
I adjusted coding and now setting RunIndex after making replacements. Still I noticed one other problem that if box with text was changed it is changing its location in OneNote page.

For example, box with text was at the page top and after few changes it went more down to the middle of page. If I am changing RunIndex why other style values are changed?


Could you please share sample code (runnable) and sample document to reproduce the issue, we will check it soon.

Just use already attached project where you’ll run application few times and you’ll see.
After I executed EXE 4-5 times I got:

Original:BeforeChanges.jpg (21.6 KB)

After making replacements: AfterChanges.jpg (13.3 KB)


Thanks for the screenshots.

We have recorded your findings with your existing ticket “NOTENET-5529” into our database. We will evaluate and look into it.

This is a known issue. It was already logged as “NOTENET-5496”. There is no ETA for it now but if this issue is equally important to you, we can rework our plans for your needs.

Yes, itwill be great if you can rework plans as without this fix I would not make any changes in OneNote documents as they’ll ruin layout so, in my opinion, this one is critical also.


Thanks for your confirmation.

Sure, we will rework our plans and get back to you once any update is available on it.

Hi Amjad,

any update/ETA here?



We are sorry but no update for the issue at the moment. However, we will check if we could provide you an ETA on it. Once we have an update on it, we will let you know here.


We are working on new public API for RichText object that will avoid error to fix styles. We plan to publish these fixes in upcoming Aspose.Note for .NET 22.5.