title: Got a message " MEMORY STREAM IS NOT EXPANDABLE" after using WordprocessingDocument base on Microsoft site on MVC
body: <p>Currently, I was base on "Search and replace text in a document part (Open XML SDK)" on the Microsoft site. I've realized that the code got an issue after the file has downloaded to my drive.</p>
  
  <p>So I opened that file and got a message </p>
  
  <blockquote>
    <p>MEMORY STREAM IS NOT EXPANDABLE   at sw.Write(docText);</p>
  </blockquote>
  
  <p>How to fix that?</p>
  
  <p>In GenerateDocxHelper class:</p>
  
  <pre><code> private readonly MemoryStream _mem;
   private Dictionary&lt;string, string&gt; _dicData;
  
   public GenerateDocxHelper(string path)
   {
       _mem = new MemoryStream(System.IO.File.ReadAllBytes(path));
       _dicData = new Dictionary&lt;string, string&gt;();
   }
  
   public MemoryStream ReplaceTextInWord()
   {
       using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(_mem, true))
          {
  
  
              string docText = null;
              using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
              {
                  docText = sr.ReadToEnd();
              }
  
  
              foreach (var data in _dicData)
              {
                  docText = docText.Replace(data.Key, data.Value);
              }
  
              using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
              {
                  sw.Write(docText);
              }
          }
  
          _mem.Seek(0, SeekOrigin.Begin);
  
          return _mem;
   }
  </code></pre>