UpdateCustomPropertyValue() throwing System.NullReferenceException:

Apr 1, 2014 at 11:45 PM
Edited Apr 1, 2014 at 11:46 PM
Hi,

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="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:fldSimple w:instr=" DOCPROPERTY  BuyerName  \* MERGEFORMAT ">
    <w:r>
      <w:t>[BuyerName]</w:t>
    </w:r>
  </w:fldSimple>
</w:fldSimple>
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
                            e.RemoveNodes();
    
                            XElement t = new XElement(w + "t", rPr, customPropertyValue);
                            Novacode.Text.PreserveSpace(t);
                            e.Add(new XElement(firstRun.Name, firstRun.Attributes(), firstRun.Element(XName.Get("rPr", w.NamespaceName)), t));
                        }
                    }
                }
                #endregion
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?

Cheers,

Paul.