Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>instead of typing plain text "taghere", insert a merge field. (in word, click insert > quick parts > field. choose "mergefield" and type "TagHere" in the "Field name" field.)</p> <p>then instead of doing a text find-replace, scan the document for merge fields and set the inner texts.</p> <pre><code>class Program { static void Main(string[] args) { string document = args[0]; using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true)) { Dictionary&lt;string, string&gt; replaceOperations = new Dictionary&lt;string, string&gt;(); replaceOperations.Add("company", "alex's applications"); replaceOperations.Add("first_name", "alexander"); replaceOperations.Add("last_name", "taylor"); //etc Replace(wordDoc, replaceOperations); } } public static char[] splitChar = new char[] {' '}; public static void Replace(WordprocessingDocument document, Dictionary&lt;string, string&gt; replaceOperations) { //find all the fields foreach (var field in document.MainDocumentPart.Document.Body.Descendants&lt;SimpleField&gt;()) { //parse the instruction string[] instruction = field.Instruction.Value.Split(splitChar, StringSplitOptions.RemoveEmptyEntries); //check if it's a merge field, and if so... if (instruction[0].ToLower().Equals("mergefield")) { //get the field name string fieldname = instruction[1]; //find the text inside (there will only be one) foreach (var fieldtext in field.Descendants&lt;Text&gt;()) { //see if we know what to set this value to string value = replaceOperations.ContainsKey(fieldname) ? replaceOperations[fieldname] : null; //if we found the replace value, set the text to this value if (value != null) fieldtext.Text = value; //should only be one text inside break; } } } } } </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