title: How to add merge field in word with docx4j?
body: <p>I am able to add text and paragraphs to word and also replace any merge fields with text but I need to add a new merge field using docx4j to create a word template. How can I do this?</p>
  
  <p>Do I use <code>wordMLPackage.getMainDocumentPart().addTargetPart(targetpart);</code> to add merge field. If so how?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>After uploading a sample word to <a href="http://webapp.docx4java.org/OnlineDemo/PartsList.html" rel="noreferrer">http://webapp.docx4java.org/OnlineDemo/PartsList.html</a> and object representing parts I probably need to add org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart object? </p>
  
  <p>But I still don't understand how to add the Mergefield. I also generated the following xml to find any clue I could get:</p>
  
  <pre><code>&lt;w:document xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:ns21="urn:schemas-microsoft-com:office:powerpoint" xmlns:ns23="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:odx="http://opendope.org/xpaths" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:ns17="urn:schemas-microsoft-com:office:excel" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:odi="http://opendope.org/components" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ns32="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:ns30="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:ns12="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:ns31="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:odq="http://opendope.org/questions" xmlns:ns8="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:odc="http://opendope.org/conditions" xmlns:oda="http://opendope.org/answers"&gt;
      &lt;w:body&gt;
          &lt;w:p w:rsidR="00F66879" w:rsidP="00E80FA3" w:rsidRDefault="003B0B3A"&gt;
              &lt;w:r&gt;
                  &lt;w:t&gt;Dear&lt;/w:t&gt;
              &lt;/w:r&gt;
              &lt;w:r w:rsidR="00E80FA3"&gt;
                  &lt;w:t xml:space="preserve"&gt; &lt;/w:t&gt;
              &lt;/w:r&gt;
              &lt;w:fldSimple w:instr=" MERGEFIELD  lastName  \* MERGEFORMAT "&gt;
                  &lt;w:r w:rsidR="005C0283"&gt;
                      &lt;w:rPr&gt;
                          &lt;w:noProof/&gt;
                      &lt;/w:rPr&gt;
                      &lt;w:t&gt;«lastName»&lt;/w:t&gt;
                  &lt;/w:r&gt;
              &lt;/w:fldSimple&gt;
              &lt;w:r w:rsidR="00E80FA3"&gt;
                  &lt;w:t&gt;,&lt;/w:t&gt;
              &lt;/w:r&gt;
          &lt;/w:p&gt;
          &lt;w:p w:rsidR="00532EBF" w:rsidP="00E80FA3" w:rsidRDefault="00532EBF"&gt;
              &lt;w:r&gt;
                  &lt;w:t&gt;On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look.&lt;/w:t&gt;
              &lt;/w:r&gt;
          &lt;/w:p&gt;
          &lt;w:p w:rsidR="00532EBF" w:rsidP="00E80FA3" w:rsidRDefault="00532EBF"&gt;
              &lt;w:r&gt;
                  &lt;w:t&gt;You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.
  &lt;/w:t&gt;
              &lt;/w:r&gt;
          &lt;/w:p&gt;
          &lt;w:p w:rsidR="001E4C13" w:rsidP="00E80FA3" w:rsidRDefault="001E4C13"&gt;
              &lt;w:r&gt;
                  &lt;w:t&gt;Thank you&lt;/w:t&gt;
              &lt;/w:r&gt;
          &lt;/w:p&gt;
          &lt;w:p w:rsidR="001E4C13" w:rsidP="00E80FA3" w:rsidRDefault="001E00E0"&gt;
              &lt;w:r&gt;
                  &lt;w:t&gt;Sincerely,&lt;/w:t&gt;
              &lt;/w:r&gt;
          &lt;/w:p&gt;
          &lt;w:p w:rsidRPr="00F66879" w:rsidR="001E00E0" w:rsidP="00E80FA3" w:rsidRDefault="00C3747B"&gt;
              &lt;w:fldSimple w:instr=" MERGEFIELD  sender  \* MERGEFORMAT "&gt;
                  &lt;w:r w:rsidR="001E00E0"&gt;
                      &lt;w:rPr&gt;
                          &lt;w:noProof/&gt;
                      &lt;/w:rPr&gt;
                      &lt;w:t&gt;«sender»&lt;/w:t&gt;
                  &lt;/w:r&gt;
              &lt;/w:fldSimple&gt;
          &lt;/w:p&gt;
          &lt;w:p w:rsidR="00E80FA3" w:rsidRDefault="00E80FA3"/&gt;
          &lt;w:sectPr w:rsidR="00E80FA3" w:rsidSect="00961253"&gt;
              &lt;w:pgSz w:w="12240" w:h="15840"/&gt;
              &lt;w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/&gt;
              &lt;w:cols w:space="720"/&gt;
              &lt;w:docGrid w:linePitch="360"/&gt;
          &lt;/w:sectPr&gt;
      &lt;/w:body&gt;
  &lt;/w:document&gt;
  </code></pre>
  
  <p>From this I can see that the MergeField is in xml element w:fldSimple so I probably need to create this xml perhaps like this:</p>
  
  <pre><code>   CTSimpleField ctSimple = factory.createCTSimpleField();
          ctSimple.setInstr(" MERGEFIELD lastname \\* MERGEFORMAT");
  
          RPr RPr = factory.createRPr();
          RPr.setNoProof(new BooleanDefaultTrue());
  
          Text t = factory.createText();
          t.setValue("fieldValue");
  
          R run = factory.createR();
          run.getContent().add(RPr);
          run.getContent().add(t);   
  
          JAXBElement&lt;CTSimpleField&gt; fldSimple = factory
                  .createPFldSimple(ctSimple);
  </code></pre>
  
  <p>Any help would be great Thanks!</p>