Null object exception using Replace

May 1, 2014 at 4:48 AM
Hi all
I have a simple .Net winforms app that
a) copies a template docx file to a destination docx file
b) performs a few replacements in the destination file of placeholder tokens with values entered in a GUI.

Essentially I am replacing the string "[replaceme]" with "abc". The source DOCX file is Word 2010 format

var xPlan = DocX.Load(iChangeFile);
xPlan.ReplaceText(sb.NavigateToFolderToken, iChangeFolder);

Initial tests were promising, and a few items got replaced with substitute values. Now I am hitting errors on almost every attempt. It appears from the contents of the revised Word doc that the replacement took place, then an attempt to remove the original text failed.

The exception is {"Object reference not set to an instance of an object."}
The stack trace is:

at Novacode.Container.GetListItemType(Paragraph p)
at Novacode.Container.get_Paragraphs()
at Novacode.DocX.get_Paragraphs()
at Novacode.Container.ReplaceText(String oldValue, String newValue, Boolean trackChanges, RegexOptions options, Formatting newFormatting, Formatting matchFormatting, MatchFormattingOptions fo)
at DeployHelper.Form1.CustomizeTemplate(String iChangeFile, String iChangeFolder) in c:\Temp\DeployHelper\Form1.cs:line 219

Any ideas on how to resolve? If I can get this to work, it will be a great time saver for me and perhaps my shop...

May 5, 2014 at 3:40 AM
Which version are you using? The compiled one or source based? If compiled please try the source based and let us know. Haven't seen a problem with such replacements before.
May 5, 2014 at 5:11 AM

Thanks Madboy, appreciate the response

I did as you suggested, and get a bit more information. The exception is thrown in the Load method of Docx, and shows as a System.IO.FileFormatException, with message of “File contains Corrupted data”.

The word doc I am trying to manipulate is 2010 format (while I have Office 2013 installed locally). When I reviewed it and displayed some of the underling recent changes, it appears they were pending some type of acceptance or rejection. (I am a Word moron). Once I stepped through the changes and Accepted/Rejected them, the Docx replaces then seemed to work fine. User error…

Again, I appreciate the feedback. An early blog for the lib makes reference to a documentation chm file, which I don’t see on the current download site. Do you know if there is any documentation available?



May 5, 2014 at 5:22 AM
I'm glad it works. As for documentation download the newest source code. It has chm file attached (not best but better then none).. You also get a 6months worth of fixes and features.