How to count the words in MS Word document with C#

1 Answer

0 votes
using System;

//Add the "Microsoft.Office.Interop.Word" extension from Project -> Add Reference...
using Microsoft.Office.Interop.Word;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Application application = new Application();
                Document document = application.Documents.Open("d:\\file.doc");

                int total_words = document.Words.Count;

                application.Quit();
                 
                Console.WriteLine("totla words: {0}", total_words); // totla words: 9083

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:
   
totla words: 9083

*/


answered Mar 19, 2015 by avibootz
edited Mar 20, 2015 by avibootz
...