Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Oh wow, I don't even want to think I understand the whole picture around this but here's a quick stab at it. Someone with more openXml experience please chime in...</p> <p>Turns out when you create a textbox in word on a docx the document.xml file get's the following markup:</p> <pre><code>&lt;w.r&gt; &lt;mc.AlertnateContent&gt; &lt;mc.Choice Requires="wps"&gt; &lt;wps:txbx&gt; &lt;w:txbxContent&gt; &lt;w:r&gt; &lt;w.t&gt; Text Goes Here &lt;/w.t&gt; &lt;/w.r&gt; &lt;/w:txbxContent&gt; &lt;/wps:txbx&gt; &lt;/mc.Choice&gt; &lt;mc.Fallback&gt; &lt;v.textbox&gt; &lt;w:txbxContent&gt; &lt;w:r&gt; &lt;w.t&gt; Text Goes Here &lt;/w.t&gt; &lt;/w.r&gt; &lt;/w:txbxContent&gt; &lt;/v.textbox&gt; &lt;/mc.Fallback&gt; &lt;/mc.AlertnateContent&gt; &lt;/w.r&gt; </code></pre> <p>Notice the mc.AlternateContent, mc.Choice, and mc.Fallback tags. What the heck are these??? Someone put it this way on a blog article I came across - </p> <p>"As I understand it - but don't take my word as gospel - AlternateContent can appear anywhere and provides a mechanism for including enhanced functionality if the consuming application can handle it, along with a fallback if it can't." -Tony Jollans - <a href="http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/f8a5c277-7049-48c2-a295-199d2914f4ba/" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/f8a5c277-7049-48c2-a295-199d2914f4ba/</a></p> <p>In my case I was only modifying the fallback textbox(v.txbx not wps.txbx) because of my mishap in assuming Resharper was right in asking me to import the DocumentFormat.OpenXml.Vml namespace for my dependency on the TextBox object. Not sure why there isn't a Textbox definition in one of my already included namespaces, DocumentFormat.OpenXml.Packaging or DocumentFormat.OpenXml.Wordprocessing but that's beyond the scope of this question. Needless to say, upon realizing this and updating my code to look for the common w.txbxContent for the two I achieved what I wanted to do.</p> <p>Here's the updated code with some refactoring, call the ReplaceTag method in the using statement from the original question and supply a model object instead of a string. Also, use the tagToValueSelector dictionary for convenience.</p> <pre><code>private void ReplaceTags(Document document, SomeModel model) { var textBoxContents = document.Descendants&lt;TextBoxContent&gt;().ToList(); foreach (var textBoxContent in textBoxContents) { ReplaceTag(textBoxContent, model); } } private void ReplaceTag(TextBoxContent textBoxContent, SomeModel model) { var tag = textBoxContent.InnerText.Trim(); if (!tagsTomValues.ContainsKey(tag)) return; var valueSelector = tagsTomValues[tag]; textBoxContent.RemoveAllChildren(); var paragraph = textBoxContent.AppendChild(new Paragraph()); var run = paragraph.AppendChild(new Run()); run.AppendChild(new Text(valueSelector(model))); } // called in the ctor private static void IntializeTags(IDictionary&lt;string, Func&lt;SomeModel, string&gt;&gt; dictionary) { dictionary.Add("&lt;&lt;IH.Name&gt;&gt;", m =&gt; string.Format("{0} {1}", m.FirstName, m.LastName)); } </code></pre> <p>Happy openXmling :)</p>
 

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