C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: Add the Microsoft.Office.Interop.Word assembly to your project. Go to Project -> Add Reference.
Next: We loop through the Words collection and read the Text property on each element. We then display and call Quit.
Word document: word.doc
One
Two
three
C# program that uses Microsoft Word interop
using System;
using Microsoft.Office.Interop.Word;
class Program
{
static void Main()
{
// Open a doc file.
Application application = new Application();
Document document = application.Documents.Open("C:\\word.doc");
// Loop through all words in the document.
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
// Write the word.
string text = document.Words[i].Text;
Console.WriteLine("Word {0} = {1}", i, text);
}
// Close word.
application.Quit();
}
}
Output
Word 1 = One
Word 2 =
Word 3 = Two
Word 4 =
Word 5 = three
Word 6 =
So: In Interop.Word, a paragraph is made up of a collection of one or more words.
Note: It is important to iterate on Words from 1 to Count inclusive. Correction suggested by Robert Ford.