This project has moved and is read-only. For the latest updates, please go here.

UpdateCustomPropertyValue() throwing System.NullReferenceException:

Apr 2, 2014 at 12:45 AM
Edited Apr 2, 2014 at 12:46 AM

I've found that a document property based mail merge we are running fails on a few of our template docx based files (set up by another member of staff). Looking closer at the error it's due to a run not being found within the element for the document property. In the cases I've found it's due to the property containing another property as shown below:
<w:fldSimple w:instr=" DOCPROPERTY  FullName  \* MERGEFORMAT " xmlns:w="">
  <w:fldSimple w:instr=" DOCPROPERTY  BuyerName  \* MERGEFORMAT ">
I suspect this is due to fault during document setup however I wouldn't expect it to cause DocX to throw an exception. The change below allows it to skip properties where no run exists that needs to be replaced.

Next thing I need to check if the inner property gets picked up correctly as it doesn't contain a namespace?

DocX.cs From Line 3778:
 #region < Word 2010
                foreach (XElement e in doc.Descendants(XName.Get("fldSimple", w.NamespaceName)))
                    string attr_value = e.Attribute(XName.Get("instr", w.NamespaceName)).Value.Replace(" ", string.Empty).Trim();
                    string match_value = string.Format(@"DOCPROPERTY  {0}  \* MERGEFORMAT", customPropertyName).Replace(" ", string.Empty);

                    if (attr_value.Equals(match_value, StringComparison.CurrentCultureIgnoreCase))
                        XElement firstRun = e.Element(w + "r");

                        if (firstRun!=null)
                            XElement firstText = firstRun.Element(w + "t");
                            XElement rPr = firstText.Element(w + "rPr");
                            // Delete everything and insert updated text value
                            XElement t = new XElement(w + "t", rPr, customPropertyValue);
                            e.Add(new XElement(firstRun.Name, firstRun.Attributes(), firstRun.Element(XName.Get("rPr", w.NamespaceName)), t));
I'd submit this change as a patch if anybody can guide me to some simple to understand instructions that show how to contribute to the project?