Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is when we use MS Word (I use the 2010 version) and then we modify the template and save it. It changes what the document contains.</p> <p>Here's what we have when we first generate the template using DocX:</p> <pre><code>&lt;w:fldSimple w:instr="DOCPROPERTY company_name \* MERGEFORMAT" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"&gt; &lt;w:r&gt; &lt;w:t&gt; &lt;w:rPr&gt; &lt;w:b /&gt; &lt;w:sz w:val="24" /&gt; &lt;w:szCs w:val="24" /&gt; &lt;w:color w:val="1F497D" /&gt; &lt;/w:rPr&gt;Company Name&lt;/w:t&gt; &lt;/w:r&gt; &lt;/w:fldSimple&gt; </code></pre> <p>And when we edit with Word (add a break line or some text) and we save it, it rewrite the fldSimple to something like that:</p> <pre><code>&lt;w:p w:rsidR="006D64DE" w:rsidRDefault="006B25B1"&gt; &lt;w:r&gt; &lt;w:fldChar w:fldCharType="begin" /&gt; &lt;/w:r&gt; &lt;w:r&gt; &lt;w:instrText&gt;DOCPROPERTY company_name \* MERGEFORMAT&lt;/w:instrText&gt; &lt;/w:r&gt; &lt;w:r&gt; &lt;w:fldChar w:fldCharType="separate" /&gt; &lt;/w:r&gt; &lt;w:r&gt; &lt;w:rPr&gt; &lt;w:b /&gt; &lt;w:color w:val="1F497D" /&gt; &lt;w:sz w:val="24" /&gt; &lt;w:szCs w:val="24" /&gt; &lt;/w:rPr&gt; &lt;w:t&gt;Company Name&lt;/w:t&gt; &lt;/w:r&gt; ... &lt;w:r&gt; &lt;w:rPr&gt; &lt;w:b /&gt; &lt;w:color w:val="1F497D" /&gt; &lt;w:sz w:val="24" /&gt; &lt;w:szCs w:val="24" /&gt; &lt;/w:rPr&gt; &lt;w:fldChar w:fldCharType="end" /&gt; &lt;/w:r&gt; &lt;/w:p&gt; </code></pre> <p>Instead of waiting someone to fix the issue, I simply tried to do a first draft of implementation. I actually modified the method UpdateCustomPropertyValue(...). I actually added the code of the first <strong>foreach</strong>. The second <strong>foreach</strong> was already there and it apply to document created from <strong>DocX</strong>.</p> <pre><code> internal static void UpdateCustomPropertyValue(DocX document, string customPropertyName, string customPropertyValue) { foreach (XElement e in document.mainDoc.Descendants(XName.Get("instrText", w.NamespaceName))) { string attr_value = e.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)) { XNode node = e.Parent.NextNode; bool found = false; while (true) { if (node.NodeType == XmlNodeType.Element) { var ele = node as XElement; var match = ele.Descendants(XName.Get("t", w.NamespaceName)); if (match.Count() &gt; 0) { if (!found) { match.First().Value = customPropertyValue; found = true; } else { ele.RemoveNodes(); } } else { match = ele.Descendants(XName.Get("fldChar", w.NamespaceName)); if (match.Count() &gt; 0) { var endMatch = match.First().Attribute(XName.Get("fldCharType", w.NamespaceName)); if (endMatch != null &amp;&amp; endMatch.Value == "end") { break; } } } } node = node.NextNode; } } } foreach (XElement e in document.mainDoc.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"); 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)); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload