How to get and print all the words in MS Word document with C#

1 Answer

0 votes
using System;

//Add the "Microsoft.Office.Interop.Word" extensions 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;

                for (int i = 1; i <= total_words; i++)
                {
                    string word = document.Words[i].Text;
                    Console.WriteLine("Word {0} = {1}", i, word);
                }

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

/*
run:

Word 1 = Abc
Word 2 = Def
Word 3 = Ghi
Word 4 = v9.9
Word 5 = .
Word 6 = 00000
Word 7 = +
Word 8 = Script
Word 9 = Format
Word 10 =
Word 11 =
Word 12 =
Word 13 = General
Word 14 = information
Word 15 =
Word 16 = The
Word 17 = [
Word 18 = Script
Word 19 = ]
Word 20 = of
Word 21 = a
Word 22 = Test
Word 23 = Java
Word 24 =
Word 25 = script
Word 26 =
Word 27 = The
Word 28 = line
Word 29 = types
Word 30 = in
Word 31 = a
Word 32 = Code
Word 33 = Base
Word 34 = File
.
.
.

*/ 


answered Mar 20, 2015 by avibootz
...