none
How to detect the count of pages in everey MS word document in a folder RRS feed

  • Question

  • Hi,

    I have a folder of about 400 docx files , and I wanna get the #Pages of each and return them in something like table or grid when I choose the container folder via clicking on a browse button.

    Please help me how can I doing so.

    Thanks so much in advance.

    Aya.


    Aya Zoghby

    • Moved by Jason Dot Wang Tuesday, September 11, 2012 7:26 AM This thread is about Word for Developers (From:Visual C# General)
    Sunday, September 9, 2012 11:15 AM

Answers

  • Hi Aya,

    Thank you for posting in the MSDN Forum.

    I've adapted your code into below.

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Word;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                DialogResult result = folderBrowserDialog1.ShowDialog();
                
                if (result == DialogResult.OK)
                {
                    string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                    MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
                   
                    Microsoft.Office.Interop.Word._Application wordApp = new Microsoft.Office.Interop.Word.Application();
                    Microsoft.Office.Interop.Word._Document document;
    
                    int numberOfPages = 0;
                    for (int i = 1; i < files.Length; i++)
                    {
                        document = wordApp.Documents.Open(files[i].ToString());
                        numberOfPages = numberOfPages + document.ComputeStatistics(WdStatistic.wdStatisticPages, false);
                        document.Close();
                    }
                                   
    
                    // Print out the result. Console will not show up.
                    textBox1.Text = String.Format("Total number of pages in document: {0}", numberOfPages);
    
                    // Close word.
                    wordApp.Quit();
    
                }
            }
    
        }
    }

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 17, 2012 8:10 AM
    Moderator

All replies

  • Hi,

    for one file you can do:

    WordprocessingDocument doc = WordprocessingDocument.Open(path, false);
    string pages = doc.ExtendedFilePropertiesPart.Properties.Pages.InnerText.ToString();

    for all of them, simply loop through all the files of that folder (before that create a list<T>, and add number of pages into it!


    Mitja

    Sunday, September 9, 2012 11:41 AM
  •        

    I have wrote the following code to solve the problem in a windows application , but I have got the following errors:


    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Word;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
          
            private void button1_Click(object sender, EventArgs e)
            {
                DialogResult result = folderBrowserDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                    MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
    
    
                    // Open a doc file.
                    Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
    
                    Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Open(files.ToString());
    
                    // Get the page count.
                    int numberOfPages = document.ComputeStatistics(WdStatistic.wdStatisticPages, false);
    
                    // Print out the result.
                    Console.WriteLine(String.Format("Total number of pages in document: {0}", numberOfPages));
    
                    // Close word.
                    wordApp.Quit();              
    
    
                }
    
            }
        }
    }
    


    But I have got the following errors:

    Error    1    Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead.    

    Error    2    The type 'Microsoft.Office.Interop.Word.ApplicationClass' has no constructors defined  

    I can't even complete the code for doing the rest of the function.I need a help please.

    Regards,

    Aya.

             

    Aya Zoghby


    • Edited by ZoghbyAya Sunday, September 9, 2012 7:02 PM
    Sunday, September 9, 2012 6:59 PM
  • Hi Aya,

    Thank you for posting in the MSDN Forum.

    I've adapted your code into below.

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Word;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                DialogResult result = folderBrowserDialog1.ShowDialog();
                
                if (result == DialogResult.OK)
                {
                    string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                    MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
                   
                    Microsoft.Office.Interop.Word._Application wordApp = new Microsoft.Office.Interop.Word.Application();
                    Microsoft.Office.Interop.Word._Document document;
    
                    int numberOfPages = 0;
                    for (int i = 1; i < files.Length; i++)
                    {
                        document = wordApp.Documents.Open(files[i].ToString());
                        numberOfPages = numberOfPages + document.ComputeStatistics(WdStatistic.wdStatisticPages, false);
                        document.Close();
                    }
                                   
    
                    // Print out the result. Console will not show up.
                    textBox1.Text = String.Format("Total number of pages in document: {0}", numberOfPages);
    
                    // Close word.
                    wordApp.Quit();
    
                }
            }
    
        }
    }

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 17, 2012 8:10 AM
    Moderator
  • Monday, September 17, 2012 10:47 AM
  • Thank you so much for your kind helpful reply.

    Regards,

    Aya.


    Aya Zoghby

    Monday, September 17, 2012 5:54 PM