title: Splitting word document into separate pages using c#
body: <p>An hour ago I been searching for a code that split word document into separate pages I found this <a href="https://stackoverflow.com/questions/11754608/how-to-split-pages-of-a-word-document-into-separate-files-in-c-sharp">question</a></p>
  
  <p>Using the code in the thread</p>
  
  <pre><code>static class PagesExtension {
      public static IEnumerable&lt;Range&gt; Pages(this Document doc) {
          int pageCount = doc.Range().Information[WdInformation.wdNumberOfPagesInDocument];
          int pageStart = 0;
          for (int currentPageIndex = 1; currentPageIndex &lt;= pageCount; currentPageIndex++) {
              var page = doc.Range(
                  pageStart
              );
              if (currentPageIndex &lt; pageCount) {
                  //page.GoTo returns a new Range object, leaving the page object unaffected
                  page.End = page.GoTo(
                      What: WdGoToItem.wdGoToPage,
                      Which: WdGoToDirection.wdGoToAbsolute,
                      Count: currentPageIndex+1
                  ).Start-1;
              } else {
                  page.End = doc.Range().End;
              }
              pageStart = page.End + 1;
              yield return page;
          }
          yield break;
      }
  }
  </code></pre>
  
  <p>I call the code above using this code</p>
  
  <pre><code>var app = new Microsoft.Office.Interop.Word.Application();
  object missObj = System.Reflection.Missing.Value;
  app.Visible = false;
  
  var doc = app.Documents.Open(fileLocation);
  int pageNumber = 1;
  
  foreach (var page in doc.Pages())
  {
      Microsoft.Office.Interop.Word.Document newDoc = app.Documents.Add(ref missObj, ref missObj, ref missObj, ref missObj);
      page.Copy();
      var doc2 = app.Documents.Add();
      doc2.Range().Paste();
      object newDocName = pageNumber.ToString() + ".docx";
      Console.WriteLine(newDocName);
      doc2.SaveAs2(newDocName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocument,
                           CompatibilityMode: Microsoft.Office.Interop.Word.WdCompatibilityMode.wdWord2010);
      pageNumber++;
  }
  app.ActiveDocument.Close();
  app.Quit();
  </code></pre>
  
  <p>But I'm getting an error in a specific document and here is the error</p>
  
  <blockquote>
    <p>This method or property is not available because no text is selected.</p>
  </blockquote>
  
  <p>What is the reason for it? i checked the document and found out that the document contains lots of spaces before the next page. How can I solve this? </p>
  
  <p>And using the code above it didn't copy the header and footer. Thank you</p>
  
  <p>Update: Error</p>
  
  <pre><code>This method or property is not available because no text is selected.
     at Microsoft.Office.Interop.Word.Range.Copy()
     at retrieveObjects(String location) in Document.cs:line 31
  </code></pre>
  
  <p>and this is the line</p>
  
  <pre><code>page.Copy();
  </code></pre>