I\'m trying to get all pages of MSWord document via Microsoft.Office.Interop.Word (I\'m using C# in VS2012). What I would like to get is List< String > Pages, where inde
A bit of a simpler solution.
Pseudo code:
Implementation:
///
/// Reads each page of the word document into a string and returns the list of the page strings.
///
public static IEnumerable ReadPages(string filePath)
{
ICollection pageStrings = new List();
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Document doc = app.Documents.Open(filePath);
long pageCount = doc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages);
int lastPageEnd = 0; // The document starts at 0.
for ( long i = 0; i < pageCount; i++)
{
// The "range" of the page break. This actually is a range of 0 elements, both start and end are the
// location of the page break.
Range pageBreakRange = app.Selection.GoToNext(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage);
string currentPageText = doc.Range(lastPageEnd, pageBreakRange.End).Text;
lastPageEnd = pageBreakRange.End;
pageStrings.Add(currentPageText);
}
return pageStrings;
}