DocX icon indicating copy to clipboard operation
DocX copied to clipboard

ArgumentOutOfRangeException when replacing Text

Open milefork opened this issue 6 years ago • 5 comments

Hi folks,

I got an error with DocX. I tried to replace placeholders with text. While doing that an ArgumentOutOfRangeException comes up reproduceable. Here is my code: https://github.com/milefork/DocxErrorSample

Greetings

milefork avatar Mar 22 '19 13:03 milefork

Downgrade to version 1.2.1 will fix temporary.

Rapiiidooo avatar Apr 05 '19 09:04 Rapiiidooo

Hi, Thank you for your sample. This is already fixed in v1.5 and up. You can test the latest version v1.6 for free for 45 days here : https://xceed.com/xceed-words-for-net/. Not sure why it didn't work in your case. I tried removing the DocX reference from Nuget et re-add it. It worked.

XceedBoucherS avatar Apr 11 '19 11:04 XceedBoucherS

@Rapiiidooo yeah in this sample downgrading fixed it temporary. But in my real case project the error will come in another row with the downgraded version.

@XceedBoucherS sorry but the paid version is not an option for me. I think your redadding case worked because the previous run succeed and the document was already modified.

milefork avatar Apr 11 '19 12:04 milefork

I ran into this error today as well. Rolling back to 1.2.1, or forward to 1.6 (I was initially on 1.5). I found that the issue was a column break immediately before one of the ReplaceText keys.

Sample code: ` Dictionary<string, string> keyval = new Dictionary<string, string>(); keyval.Add("Key1", "1"); keyval.Add("Key2", "2"); keyval.Add("Key3", "3"); var sampleDoc = DocX.Load(@".\SampleFile.docx");

foreach (var item in keyval) { sampleDoc.ReplaceText(item.Key, item.Value); // Exception here }

sampleDoc.Save(); `

SampleFile.docx

Work-around was to reformat the document so the exception does not occur.

JoeCoDev avatar Dec 20 '19 19:12 JoeCoDev

Hi, Thank you for reporting this. Yes, this problem occurs because of the column break preceding a text to replace. In v1.7, you won't see the ArgumentOutOfRangeException anymore. But the text preceding the column break won't be completely replaced (the new text will be added but the old one won't be removed). In v1.8, this will all be fixed.

Thank you.

XceedBoucherS avatar Jan 06 '20 17:01 XceedBoucherS