title: How to merge two word documents which are saved with .docx to a third file?
body: <p>I am trying to merge two documents lets say 
  Document 1: Merger1.doc
  Document 2: Merger2.doc</p>
  
  <p>I would like to store it into a new file doc2.docx.</p>
  
  <p>I have used this piece of code to do this, but it is throwing some error.</p>
  
  <p>CODE:</p>
  
  <pre><code>import java.io.*;
  import org.apache.poi.hwpf.HWPFDocument; 
  import org.apache.poi.hwpf.usermodel.CharacterRun;
  import org.apache.poi.hwpf.usermodel.Range;
  
  public class MergerFiles {
  
  public static void main (String[] args) throws Exception {  
      // POI apparently can't create a document from scratch,  
      // so we need an existing empty dummy document  
      HWPFDocument doc = new HWPFDocument(new FileInputStream("C:\\Users\\pallavi123\\Desktop\\Merger1.docx"));  
      Range range = doc.getRange();  
  
  
      //I can get the entire Document and insert it in the tmp.doc  
      //However any formatting in my word document is lost.  
      HWPFDocument doc2 = new HWPFDocument(new FileInputStream("C:\\Users\\pallavi123\\Desktop\\Merger2.docx"));  
      Range range2 = doc2.getRange();  
      range.insertAfter(range2.text());  
  
      //I can get the information (text only) for each character run/paragraph or section.  
      //Again any formatting in my word document is lost.  
      HWPFDocument doc3 = new HWPFDocument(new FileInputStream("D:\\doc2.docx"));  
      Range range3 = doc3.getRange();  
      for(int i=0;i&lt;range3.numCharacterRuns();i++){  
          CharacterRun run3 = range3.getCharacterRun(i);  
          range.insertAfter(run3.text());  
      }  
  
      OutputStream out = new FileOutputStream("D:\\result.doc");  
      doc.write(out);  
      out.flush();  
      out.close();  
  }  
  }  
  </code></pre>
  
  <p>ERROR CODE:</p>
  
  <pre><code>Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
  at org.apache.poi.poifs.storage.HeaderBlockReader.&lt;init&gt;(HeaderBlockReader.java:108)
  at org.apache.poi.poifs.filesystem.POIFSFileSystem.&lt;init&gt;(POIFSFileSystem.java:151)
  at org.apache.poi.hwpf.HWPFDocument.verifyAndBuildPOIFS(HWPFDocument.java:120)
  at org.apache.poi.hwpf.HWPFDocument.&lt;init&gt;(HWPFDocument.java:133)
  at MergerFiles.main(MergerFiles.java:11)
  </code></pre>
  
  <p>Am i missing any jar file or the way am using code is wrong. Need your valuable suggestions.</p>
  
  <p>Thanks in Advance.</p>