none
Count pages in a document

    Question

  • I am using the following code to count the number of pages in an active Word 2010
    document.<o:p></o:p>

    var NumberOfPages = aDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages,
    ref oMissing); <o:p></o:p>

    The result returns 2 but there is only 1 page in the document.<o:p></o:p>

    Page 1 of the document is full.<o:p></o:p>

    Is there anything that I am missing?<o:p></o:p>


                                    


    Leonard Swarczinski Software Developer Postal Center International

    Wednesday, July 24, 2013 7:04 PM

Answers

  • Hi Leanard

    If you PRINT the document, how many pages does it print?

    And if you view it in PrintPreview?

    Which version of Word?


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, August 01, 2013 4:02 PM
    Moderator

All replies

  • Hi Leonard,

    I can able to get the exact number of pages in a word document using the following code. I have tried the same way as like you did. In addition to your code, I have invoked the Repaginate() method of Document class in order to update the exact number of pages in document properties collection. Please try this below code and let me know whether it works for you.

                int pageCount = -1;
                Word.Application application = new Word.Application();
                Word.Document document = application.Documents.Open(ref fileName);
                document.Activate();
                try
                {
                    application.ActiveDocument.Repaginate();
                    Word.WdStatistic statPageCount = Word.WdStatistic.wdStatisticPages;
                    pageCount = document.ComputeStatistics(statPageCount);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message + "\n" + exp.Source);
                }
                finally
                {
                    document.Close();
                    application.Quit();
                }
                MessageBox.Show(string.Format("There are {0} pages in this word document", pageCount.ToString()));

    Best, 

    Vijayakumar Srinivasan


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Thursday, July 25, 2013 5:39 AM
  • Hi Leonard,

    It seems the issue cannot be reproduced on my side. My step like below.

    1.  Create a new .docx file and write the word ‘Hello’ in first page
    2.  Run the code like below
     
     

    public static void CountWordPage(string path)
            {
                Application word = new Application();
                Document doc=word.Documents.Open(path);
                Console.WriteLine("{0}  pages count:{1}",path, word.ActiveDocument.ComputeStatistics(WdStatistic.wdStatisticPages, Type.Missing));
                doc.Close();
              }

    3.  The result returned was 1
    4.  Then I inserted a blank page , the resoult was 2.
    5.  At last I removed the blank page, it returned 1.

    If it doesn't work for you, please check your .docx file and let me know.

    Best Regards,
    Fei


    Fei
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 25, 2013 6:31 AM
    Moderator
  • Hi Leanard

    If you PRINT the document, how many pages does it print?

    And if you view it in PrintPreview?

    Which version of Word?


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, August 01, 2013 4:02 PM
    Moderator