Note that there are some explanatory texts on larger screens.

plurals
  1. POReplacing InnerText of a TextBox
    primarykey
    data
    text
    <p>Using the MS OpenXml Sdk I have been able to copy a templatestream to a resultstream and append dynamic text(w.p>>w.r>>w.t) at the end of the body using the following code:</p> <pre><code>var templateStream = File.OpenRead(templatePath); templateStream.CopyTo(resultStream); using (var resultPackage = WordprocessingDocument.Open(resultStream, true)) { var document = resultPackage.MainDocumentPart.Document; var body = document.Body; // Add new text. var para = body.AppendChild(new Paragraph()); var run = para.AppendChild(new Run()); run.AppendChild(new Text(firstName)); document.Save(); } </code></pre> <p>My next logical step was to then replace the innertext of a textbox in the resultStream with the firstName as in the code below.</p> <pre><code>// replacing code in using statement from above var document = resultPackage.MainDocumentPart.Document; var textbox = document.Descendants&lt;TextBox&gt;().First(); const string firstNametag = "&lt;&lt;IH.FirstName&gt;&gt;"; if (textbox.InnerText.Contains(firstNametag)) { var textboxContent = textbox.Elements&lt;TextBoxContent&gt;().First(); textboxContent.RemoveAllChildren(); var paragraph = textboxContent.AppendChild(new Paragraph()); var run = paragraph.AppendChild(new Run()); run.AppendChild(new Text(firstName)); } document.Save(); </code></pre> <p>In the first example and with some additional code the result stream is appropriately serialized to a docx and the firstName is appended to the end of the body when viewed in Word. In the second example though the textbox and its contents remain the same even though further examination in the debugger showed the textboxContent's children reflecting the changes made above.</p> <p>I am new to OpenXML development so if there is anything obvious please point it out.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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